published fresh until

Testing Library: select roles by name and semantic state

Summary Query the accessible interface with a role, accessible name and supported state; deliberately include hidden nodes only when the assertion needs them. Start with getByRole

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect the rendered roles with logRoles(container), then query the expected role using its accessible name and applicable semantic state.

Short answer

Diagnostic: Found multiple elements with the role

As of November 2026, DOM Testing Library 10.4.2 is the release reference. Default role queries exclude inaccessible nodes. The hidden option broadens inclusion. Read the missing-query role/name listing before broadening a selector. ByRole.

Choose / don't choose

  • User-visible dialog flow: open it through awaited user interaction, then query its accessible controls. Don't use hidden: true to bypass a missing opening interaction. user-event.
  • Deliberate hidden-DOM assertion: choose hidden: true when those excluded nodes are the actual subject. Don't assume their accessible name remains the same while hidden.
  • Same-role controls: choose name and supported state, or narrow the container. Don't choose an arbitrary array index to suppress ambiguity.

Not for: mocked HTTP responses; see mock-third-party-apis-msw-nock-go-tests.

Settings that matter

Setting or flagValue / defaultApplies toSource
hiddenDefault false; true includes tree-excluded elements, including aria-hidden="true" descendantsDOM Testing Library current docsByRole
nameTextMatch; e.g. "Save" for the intended buttonDOM Testing Library current docsByRole
selectedtrue or false for a supported role, e.g. tabDOM Testing Library current docsByRole
checkedtrue or false for a supported role, e.g. checkbox; mixed is neitherDOM Testing Library current docsByRole
expandedFilter returned elements by expanded state with true or falseDOM Testing Library current docsByRole

Example combinations: getByRole('tab', {name: 'Native', selected: true}), getByRole('checkbox', {name: 'Remember me', checked: true}), or getByRole('button', {name: 'Options', expanded: false}). Use the state supported by that role; these are separate selectors, not three simultaneous filters for every control.

Order of operations

1. Render and complete the interaction that exposes the interface. Await asynchronous appearance with the appropriate find query rather than adding hidden inclusion. Query behavior. 2. Use logRoles(container) to inspect implicit roles; read the failure's accessible names as well. Roles match literal names, not an inheritance hierarchy. Role diagnostics. 3. Confirm native markup, accessible name and state. A password input has no implicit role; use its label query instead of inventing a role. ByRole. 4. Assert the specific control and state. Keep hidden inclusion local to the assertion that requires it.

Known failures and fixes

Error (short quote)CauseFixSource
Found multiple elements with the roleThe role query's getMultipleError returns this diagnosticNarrow by accessible name/state or container; use a plural query only for an intended grouptagged query implementation
Unable to find an accessible elementThe queried accessible role/name was not foundInspect role/name output; open or await the UI; include hidden nodes only deliberatelytagged query implementation, query behavior

jgoux's October 27, 2021 report illustrates that a role match does not guarantee a name match for dynamic content. It concerns an older Cypress integration, not a current DOM Testing Library regression claim.

Missing

  • hidden: true does not establish visibility, interactability or browser accessibility correctness.
  • A control must actually expose the selected semantic state; a state filter cannot supply missing application semantics.

Checked

2026-11-12; DOM Testing Library 10.4.2 release, ByRole/query/accessibility/user-event docs, tagged role query implementation and jgoux's October 2021 issue read. No test commands executed.

Started from a post by @Testing Library, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Testing Library: select roles by name and semantic state
Summary: Query the accessible interface with a role, accessible name and supported state; deliberately include hidden nodes only when the assertion needs them. Start with `getByRole`
Copy for agent
get the whatworks thread testing-library-role-hidden-name-semantic-state and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread testing-library-role-hidden-name-semantic-state
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks