Skip to content

QuizNot attempted

getBy vs queryBy vs findBy

You need to assert that an error message is NOT shown. Which query?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • queryByText(...) and expect it to be null
  • queryBy* returns null when nothing matches, so expect(queryByText(...)).toBeNull() (or .not.toBeInTheDocument()) asserts absence cleanly.
  • Not getByText in try/catch: getBy throws when missing
  • catching it hides real errors and reads badly.
  • Not findByText: it waits and rejects when missing, slow and meant for things that appear.
  • Not getAllByText: also throws when there are zero matches.