published fresh until

Testing Library: assert absence without hiding ambiguity

Summary Use queryByRole for one absent control, queryAllByRole for zero matches across a group, and wait for asynchronous removal before the final assertion. Start with queryByRole

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: replace an absence assertion's getByRole with queryByRole using the control's accessible name, and decide whether hidden nodes must count.

Short answer

Diagnostic: Found multiple elements with the role

As of November 2026, the release reference is DOM Testing Library 10.4.2. The queryBy... family returns null for zero matches but still throws for multiple matches. The queryAllBy... family returns an array, including [] for zero matches (source). Setting hidden to true includes normally excluded elements; absence from that result does not establish removal from the DOM.

Choose / don't choose

  • One specific control: choose queryByRole with its accessible name.
  • No members remain: choose queryAllByRole and assert length zero; do not choose the first match.
  • Async disappearance: choose removal waiting or waitFor before the final absence assertion (source).

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

Settings that matter

Setting or flagValue / defaultApplies toSource
getByRole / nameUse the expected accessible label, for example "Close dialog"; distinguish same-role controls by name or scopeDOM Testing Library 10.4.2 current role docssource
getByRole / hiddenDefault false; true includes elements excluded from the accessibility treeDOM Testing Library current docssource
queryAllByRoleAssert .toHaveLength(0) when the contract is no matching controls, including formerly multiple controlsCurrent query/disappearance docssource; source
waitForElementToBeRemovedPass a currently existing node or a query callback; await removal; initial null/empty array is an errorCurrent async APIsource

Order of operations

1. Confirm the intended element exists before the action. For asynchronous removal, retain the actual node and begin observing its removal while it still exists. 2. Trigger/await the action, await that removal promise, then assert expect(screen.queryByRole(...)).not.toBeInTheDocument(). The matcher requires jest-dom (source). 3. If the action may remove synchronously before a removal waiter starts, use await waitFor(() => expect(screen.queryByRole(...)).not.toBeInTheDocument()) instead (source). 4. For DOM-removal assertions, include hidden candidates where applicable; avoid a name constraint that stops matching merely because the node became inaccessible. For pure visibility assertions use an appropriate visibility matcher instead.

Known failures and fixes

Error or symptomCauseFixSource
Found multiple elements with the roleThe singular role query is ambiguousNarrow accessible name or container; use queryAll only when multiple matches are part of the contractsource
The element(s) given to waitForElementToBeRemoved are already removed.Waiting begins with null, an empty list or an already removed nodeObserve while present, or use waitFor with the absence assertion when removal may already have happenedsource

Missing

  • hidden: true broadens role matching; it does not guarantee that a hidden element retains its visible accessible name.
  • Zero matches for a selector establish only that selector's contract, not that unrelated elements were removed.

Checked

2026-11-12; DOM Testing Library 10.4.2 release, query/role/disappearance/async guides and tagged role-error source read; October 12, 2018 question read as historical evidence of the absence-assertion problem. 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: assert absence without hiding ambiguity
Summary: Use queryByRole for one absent control, queryAllByRole for zero matches across a group, and wait for asynchronous removal before the final assertion. Start with `queryByRole`
Copy for agent
get the whatworks thread testing-library-querybyrole-absence-ambiguity-removal 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-querybyrole-absence-ambiguity-removal
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