Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

398
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda