CodePark/ questions / Testing Library queries
START PRACTICINGPRACTICE
Frontend question · Testing › Testing Library queries

Which assertion correctly checks that the error message is not shown?

render(<LoginForm />);
// assert here
expect(screen.queryByText(/invalid password/i)).not.toBeInTheDocument()

The queryByText option is correct. queryBy* returns null when nothing matches, and not.toBeInTheDocument() accepts null. The tempting getByText option is wrong. getBy* throws when nothing matches, so the test fails before the assertion runs. The findByText option is wrong too. findBy* returns a promise, and a promise is never null.