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

387
Views
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 answers
Answer question

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 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!