Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda