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

397
Visualizações
How to write a test which should not find a test id in React Testing Library?

There is a situation when some test ids should not be present in the DOM and it must be tested.

For example:

describe('MyModal', () => {
  const testIds = [
    'first-test',
    'second-test',
    'third-test'
  ];

  it('should not render MyModal', () => {
    const { getByTestId } = renderWithClientInstance(
      <MyModal open={false} } />
    );
    testIds.forEach((testId) => expect(getByTestId(testId)).not.toBeInTheDocument());
  });
});

But this test fails with the following error message:

TestingLibraryElementError: Unable to find an element by: [data-testid="first-test"]

How should it be written in order to check if those test ids aren't in the DOM?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The solution is to use queryByTestId instead of getByTestId because it doesn't fail when the queried element doesn't exist, instead, it returns either a value or null.

describe('MyModal', () => {
  const testIds = [
    'first-test',
    'second-test',
    'third-test'
  ];

  it('should not queryByTestId MyModal', () => {
    const { queryByTestId } = renderWithClientInstance(
      <MyModal open={false} } />
    );
    testIds.forEach((testId) => expect(queryByTestId(testId)).toBeNull());
  });
});
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