Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Testing Library: Unable to find an element with the text /learn react/i in a new React app test

ErrorTestingLibraryElementError: Unable to find an element with the text: /learn react/i

Short answer

The starter test looks for the link text learn react, which exists only in the original template. Once App renders something else, getByText throws. Change the query to text your component really renders, wait for it with findByText when it appears later, and run the test on CI the same way.

  • Applies to React 19, Testing Library, Jest
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the single test and read the printed DOM under the error; it shows what is really rendered.

    npx jest src/App.test.js
  2. 2Match what you see, then jump to its fix

    1. 1The printed DOM shows your own page and nothing like the learn react link.App no longer renders the template textFix
    2. 2The text is visible in the browser but missing at the moment of the query.The text appears after an async stepFix
    3. 3The text exists in the DOM output, yet the query fails.The query is skipped or too strictFix

What does the error look like?

  • TestingLibraryElementError: Unable to find an element with the text: /learn react/i

Cause 1 of 3

App no longer renders the template text

1Check

getBy queries throw a descriptive error when nothing matches. The template test checks for a link that your edited App no longer has.

cat src/App.test.js

2Fix

Query for text or a role that your component renders now.

import { render, screen } from '@testing-library/react';
import App from './App';

test('shows the heading', () => {
  render(<App />);
  expect(screen.getByText(/my dashboard/i)).toBeInTheDocument();
});

3Verify

Run the test again.

npx jest src/App.test.js

Confirmed by testing-library.com

Did this fix it?

Cause 2 of 3

The text appears after an async step

1Check

getBy is synchronous. findBy returns a promise that retries until the element appears; its default timeout is 1000 ms.

grep -n "getByText\|findByText" src/App.test.js

2Fix

Make the test async and use findByText for text that arrives after a fetch or effect.

test('shows loaded data', async () => {
  render(<App />);
  expect(await screen.findByText(/welcome back/i)).toBeInTheDocument();
});

3Verify

Run the test with the slower case in mind, for example on CI.

npx jest src/App.test.js

Confirmed by testing-library.com

Did this fix it?

Cause 3 of 3

The query is skipped or too strict

1Check

getByText matches elements whose own text node matches, ignores script and style elements by default, and matches exactly unless told otherwise.

npx jest src/App.test.js 2>&1 | head -60

2Fix

Match with a case-insensitive regex, set exact to false, or narrow with selector. The ignore option controls which elements are skipped.

screen.getByText(/learn react/i, { exact: false });

3Verify

Run the test again.

npx jest src/App.test.js

Confirmed by testing-library.com

Did this fix it?

How do I stop it coming back?

  • Replace the starter test as soon as you change App, so CI does not fail on stale text.
  • Prefer role queries (getByRole) for buttons and links; they follow what users see.
  • Use findBy for anything fetched, and getBy only for what is there at first render.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.