When testing components with React Testing Library, I find myself starting with getBy*
, and occasionally needing to replace it with queryBy*
(for example if I need to check for the non-existence of an element). My tests end up with a mix of getBy
and queryBy
, and I've recently just been using queryBy
for everything.
It got me thinking... is there ever a reason to use getBy
?
Assertions like this fail as expected, without the need to throw an error:
expect(queryByText('Click me')).toBeInTheDocument();
expect(queryByLabel('Name').value).toBe('George')
What's the advantage of throwing an error if an element isn't found, and is there a reason not to use queryBy
for all (synchronous) queries?
EDIT: It looks like queryBy
is now only recommended for asserting that something is not in the document:
The article also recommends using screen.queryBy
/screen.getBy
rather than destructuring from render
, which simplifies changing from one to another, since you no longer have to update the destructured function.