Reasons to use getBy* in conjunction with toBeInTheDocument
04:48 18 Dec 2022

There are plenty of examples of getBy* or findBy* queries are used in conjunction with expect().toBeInTheDocument(). At the same time the Testing Library docs say that both of those type of queries throw errors when 0 elements match.

So there is a situation that when we do expect(screen.getByText('some text')).toBeInTheDocument() we're checking the element presence twice, which is redundant.

What's the reason everyone's doing this?

reactjs react-testing-library jest-dom