getByRole('generic') finds no div or span in a Testing Library test
Fix it fast
Most common cause, 1 of 3The query filters a generic element by name
Use the query that fits plain containers: Testing Library's own priority list names getByText for non-interactive elements such as div and span.
import { render, screen } from '@testing-library/react';
render(<Cart items={3} />);
// instead of getByRole('generic', { name: 'Total: 3 items' })
expect(screen.getByText('Total: 3 items')).toBeInTheDocument();Short answer
Testing Library throws Unable to find an accessible element with the role "generic" when no div, span or similar element passes every filter of the query. Generic elements never take a name from their text, so a name option cannot match, and hidden elements are skipped unless hidden: true is set. Query by text instead. Applies to @testing-library/dom 10.
Which cause do you have?
Do not look for your div in the list of roles printed under the error: Testing Library drops the generic role from that printout on purpose. Instead, count the generic elements with every filter removed. Zero points to the third cause; any other number means the name or visibility filter removed the match.
// temporary line in the failing test, after render()
console.log(screen.queryAllByRole('generic', { hidden: true }).length);Then find the line you see in this list and open its fix:
- 1The query filters a generic element by name
The message ends with and name "..." after the role - 2The element is hidden from the accessibility tree
Under the error: There are no accessible roles. But there might be some inaccessible roles. - 3A role attribute gives the element a different role
The count from the first step is 0, yet screen.debug() shows the element with a role attribute such as role="status"
What does the error look like?
Unable to find an accessible element with the role "generic"Unable to find an accessible element with the role "generic" and name "Total"There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`.
Cause 1 of 3
The query filters a generic element by name
You seeThe message ends with and name "..." after the role
Check
Unable to find an accessible element with the role "generic" and name "..." comes from the name filter. ARIA lists generic among the roles that cannot be named, and the name calculation Testing Library relies on takes text content into account only for roles such as button, link or heading. A div or span with no aria-label or title therefore has an empty accessible name, whatever text it holds, and no name string or regular expression matches it.
screen.debug(screen.getByText('Total: 3 items'));Fix
Use the query that fits plain containers: Testing Library's own priority list names getByText for non-interactive elements such as div and span. Assert on the text, or on the element getByText returns.
import { render, screen } from '@testing-library/react';
render(<Cart items={3} />);
// instead of getByRole('generic', { name: 'Total: 3 items' })
expect(screen.getByText('Total: 3 items')).toBeInTheDocument();Verify
The test passes, and the same query keeps working if the wrapper element later changes from div to span.
npx jest Cart.testConfirmed by w3.org
Cause 2 of 3
The element is hidden from the accessibility tree
You seeUnder the error: There are no accessible roles. But there might be some inaccessible roles.
Check
ByRole leaves out every element that is excluded from the accessibility tree unless the query sets hidden: true, so a hidden div produces the same Unable to find message. Testing Library treats an element as excluded when it, or any ancestor, has the hidden attribute, aria-hidden="true" or display: none, and also when its computed visibility is hidden.
const el = screen.getByText('Total: 3 items');
console.log(el.closest('[hidden],[aria-hidden="true"]'));Fix
If the element is meant to be hidden at this point, find it with getByText, which does not skip hidden nodes, and assert that it is not visible. If it should be visible, the component is in a state you did not expect, so set up the props or the user action that shows it first.
// the summary stays hidden until the cart is opened
expect(screen.getByText('Total: 3 items')).not.toBeVisible();Verify
The assertion passes, and it fails as soon as the element becomes visible, which is what the test should prove.
npx jest Cart.testConfirmed by testing-library.com
Cause 3 of 3
A role attribute gives the element a different role
You seeThe count from the first step is 0, yet screen.debug() shows the element with a role attribute such as role="status"
Check
The generic role cannot be found on an element that carries a role attribute. When that attribute is present, ByRole compares the query only with the first token written in it and ignores the tag's implicit role, so <div role="status"> answers to status and never to generic.
screen.debug();Fix
Query the role that the attribute declares, and use its name if the element has an aria-label.
// <div role="status" aria-label="Save state">Saved</div>
expect(screen.getByRole('status', { name: 'Save state' })).toHaveTextContent('Saved');Verify
The role query finds the element on the first try, with no fallback to test ids.
npx jestConfirmed by github.com
How do I stop it coming back?
- Use getByText or within() for layout wrappers, and keep getByRole for elements that have a nameable role.
- Add role and aria-label only where they describe the element for assistive technology, then query by that role in tests.
- Assert hidden states with jest-dom's toBeVisible() so a test says why an element is missing instead of relying on ByRole skipping it.
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 on the same stack: TaxiDex, QistLock, Taskvare.