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

203
Views
¿Cuál es la diferencia entre toBeInTheDocument y getBy* en @testing-library/react?

¿Hay alguna diferencia entre

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

Y

 screen.getByText('<something>');

(Las getBy* y queryBy* no son relevantes)

¿En react-testing-library ?

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

0

getByText intenta encontrar el nodo y arroja un error cuando no puede encontrarlo. Esto hará que la prueba falle inmediatamente.

queryByText , por otro lado, devolverá un null si no puede encontrar el nodo.

Supongamos que tiene texto <something> en el componente que se representó, puede afirmar si se ha representado o no.

Si está seguro de que el texto se ha representado, simplemente puede usar getByText

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

y la prueba pasaría.

Si para el escenario el texto no se representó, la afirmación anterior generará un error y fallará el caso de prueba.

En tales casos, el texto queryByText tiene más sentido

Cuando no se ha prestado

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

Comparación de salida

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!