Maaz Zindani

Loading the index…

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

getBy query fails in React Testing Library because several elements match

ErrorTestingLibraryElementError: Found multiple elements

Short answer

getBy and queryBy throw when more than one element matches, and findBy rejects. Narrow the query with the name option of getByRole or by scoping with within. If several matches are expected, use getAllBy. If you see duplicates you did not render, earlier tests left DOM behind: check automatic cleanup.

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

Fix it fast

  1. 1Find out which cause you have

    Read which elements the error lists, then print the DOM to see why there are several.

    screen.debug()
  2. 2Match what you see, then jump to its fix

    1. 1The error lists several elements of the same role or text, all legitimately renderedThe query is too broad for the pageFix
    2. 2A list, table or repeated card renders by designThe match is expected to be several elementsFix
    3. 3Both a nav link and a heading have the same labelThe same text appears in two regions of the pageFix
    4. 4Duplicates appear only when several tests run, and the single test passes aloneEarlier tests left their render in the documentFix

What does the error look like?

  • TestingLibraryElementError: Found multiple elements

Cause 1 of 4

The query is too broad for the page

1Check

Per the Testing Library docs, getBy throws a descriptive error when more than one element matches.

screen.getAllByRole('button').map(b => b.textContent)

2Fix

Add the name option so the role query matches one accessible name.

screen.getByRole('button', { name: 'Close' })

3Verify

Re-run the test.

npx jest path/to/file.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 2 of 4

The match is expected to be several elements

1Check

The docs provide getAllBy, queryAllBy and findAllBy for several matches. queryAllBy returns an empty array when there is none.

screen.getAllByRole('listitem')

2Fix

Use getAllBy and assert on the array.

const items = screen.getAllByRole('listitem');
expect(items).toHaveLength(3);

3Verify

Run the test; the assertion on length passes.

npx jest path/to/file.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 3 of 4

The same text appears in two regions of the page

1Check

within limits a query to the children of one element, and screen is bound to document.body.

screen.debug()

2Fix

Scope the query with within to the region you mean.

import { within } from '@testing-library/react';

const nav = screen.getByRole('navigation');
within(nav).getByRole('link', { name: 'Home' });

3Verify

Run the test.

npx jest path/to/file.test.jsx

Confirmed by testing-library.com

Did this fix it?

Cause 4 of 4

Earlier tests left their render in the document

1Check

RTL's setup docs say cleanup runs after each test automatically only when the test framework provides a global afterEach.

npx jest path/to/file.test.jsx -t 'test name'

2Fix

Make sure the framework exposes afterEach globally (for Vitest set globals: true), or call cleanup yourself in afterEach. Also check RTL_SKIP_AUTO_CLEANUP is not set.

import { cleanup } from '@testing-library/react';
afterEach(cleanup);

3Verify

Run the whole file and confirm no duplicates.

npx jest path/to/file.test.jsx

Confirmed by testing-library.com

Did this fix it?

How do I stop it coming back?

  • Prefer getByRole with name; it matches how users find elements.
  • Use within for repeated regions.
  • Leave automatic cleanup on.

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.