Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Whats the difference between toBeInTheDocument and getBy* in @testing-library/react

Is there a difference between

expect(screen.queryByText('<something>')).toBeInTheDocument();

And

screen.getByText('<something>');

(The specific getBy* and queryBy* operation are not relevant)

In react-testing-library?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getByText tries to find the node and throws an error when it is not able to find it. This will cause the test to fail immediately.

queryByText on the other hand will return null if it is unable to find the node.

Let's suppose you have text <something> in the component that was rendered, you can assert if it has been rendered or not.

If sure that the text has been rendered, then you can simply use getByText

expect(screen.getByText('<something>')).toBeInTheDocument();

and the test would pass.

If for the scenario the text did not render then the above assertion will thrown an error and fail the test case.

In such cases queryByText text makes the most sense

When has not been rendered

expect(screen.getByText('<something>')).not.toBeInTheDocument(); // throws an error

expect(screen.queryByText('<something>')).not.toBeInTheDocument(); // asserts as true

Output comparison

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!