After reviewing the documentation, it was noted that queries made using findBy
return a Promise. Interestingly, utilizing these queries with Promise.prototype.catch()
seems ineffective in comparison to pairing them with async/await + try...catch.
An instance where 'not found' is correctly logged can be seen below:
const { screen } = require('@testing-library/dom');
beforeAll(() => {
document.body.innerHTML = `
<header></header>
`;
});
test('DOM', async () => {
try {
await screen.findByRole('aaaaa');
console.log('found');
} catch {
console.log('not found');
}
});
However, no logs are shown in this scenario:
test('DOM', () => {
screen.findByRole('aaaaa')
.then(() => {
console.log('found');
})
.catch(() => {
console.log('not found');
});
});
Could there be an explanation for this difference in behavior?