Sub topic 6
Debugging when a query cannot find an element
This chapter assumes you can render a component with render, query with screen, and know the difference between getBy, queryBy, and findBy. When a query fails, Testing Library gives you enough information to find the cause without guessing. You only need to know where to look.
Read the error first
A failing getBy query throws an error that includes the query, a hint, and a snapshot of the DOM at that moment. Here is a component and a test that fails on purpose:
// Greeting.jsx
export function Greeting() {
return (
<section>
<h1>Welcome back</h1>
<button>Log out</button>
</section>
);
}
// Greeting.test.jsx
import { render, screen } from '@testing-library/react';
import { Greeting } from './Greeting';
test('finds the sign in button', () => {
render(<Greeting />);
screen.getByRole('button', { name: 'Sign in' });
});The error looks roughly like this:
TestingLibraryElementError: Unable to find an accessible element with the role "button" and name "Sign in"
Here are the accessible roles:
heading:
Name "Welcome back":
<h1 />
button:
Name "Log out":
<button />The role button exists, but its name is "Log out". The query was wrong, not the component. Reading the list of roles and names is usually enough to fix the problem.
Print the DOM with screen.debug
screen.debug() prints the current DOM to the console. Call it just before the failing query.
import { render, screen } from '@testing-library/react';
import { Greeting } from './Greeting';
test('inspects the DOM', () => {
render(<Greeting />);
screen.debug(); // whole document body
screen.debug(screen.getByRole('heading')); // one element only
});Pass an element to print only that part. Long output is truncated at about 7000 characters by default. Raise the limit with the DEBUG_PRINT_LIMIT environment variable.
List roles with logRoles
When a ByRole query fails, you may not know which roles the browser assigns to your markup. logRoles prints every role in a container, along with the element that owns it.
import { render, logRoles } from '@testing-library/react';
import { Greeting } from './Greeting';
test('lists roles', () => {
const { container } = render(<Greeting />);
logRoles(container);
});Use it to check that a div with a click handler has no role, or that a section without a label is not exposed as a region.
Common causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Role exists, name does not match | Wrong accessible name or text | Compare against the listed names |
| Element not found right after an action | The update is asynchronous | Use `findBy` or `waitFor` |
| `getBy` fails for an element that should be absent | `getBy` throws when nothing matches | Use `queryBy` for absence checks |
| Text query fails on split text | Text is spread over several elements | Match a smaller piece, or query by role |
| Element is in the DOM but `ByRole` skips it | It is hidden with `display: none`, `hidden`, or `aria-hidden` | Pass `{ hidden: true }` only if that is the intent |
| Multiple elements found | The query matches more than one node | Narrow it with `name`, or use `getAllBy` |
A debugging workflow
- Read the error message and compare the query with the listed roles or DOM.
- Add
screen.debug()before the query to see the state at that moment. - If the element appears later, switch to
findByand confirm the test passes. - If the element should never match, check its markup with
logRoles. - Remove the debug calls when the test passes.
Exercise
A test calls screen.getByText('Saved') right after clicking a Save button. The component shows "Saved" after a network call resolves. The test fails with "Unable to find an element". What is the likely cause, and how do you fix it?
Show answer
The "Saved" text is not in the DOM yet when the synchronous getByText runs, because the update happens after an async call. Use await screen.findByText('Saved'), which retries until the element appears or a timeout passes. Calling screen.debug() before the query would show the DOM without the text, which confirms the cause.