Testing Library: Unable to find an element with the text /learn react/i in a new React app test
TestingLibraryElementError: Unable to find an element with the text: /learn react/iShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The printed DOM shows your own page and nothing like the learn react link.App no longer renders the template textFix - 2
The text is visible in the browser but missing at the moment of the query.The text appears after an async stepFix - 3
The text exists in the DOM output, yet the query fails.The query is skipped or too strictFix
- 1
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.js2Fix
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.jsConfirmed by testing-library.com
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.js2Fix
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.jsConfirmed by testing-library.com
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 -602Fix
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.jsConfirmed by testing-library.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
React Testing Library cannot find an element by its data-testid
Unable to find the element with data-testid
getByTestId looks only at the DOM that exists right now, and only at the attribute named data-testid.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix