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
queryByRolewith its accessible name. - No members remain: choose
queryAllByRoleand assert length zero; do not choose the first match. - Async disappearance: choose removal waiting or
waitForbefore 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 flag | Value / default | Applies to | Source |
|---|---|---|---|
getByRole / name | Use the expected accessible label, for example "Close dialog"; distinguish same-role controls by name or scope | DOM Testing Library 10.4.2 current role docs | source |
getByRole / hidden | Default false; true includes elements excluded from the accessibility tree | DOM Testing Library current docs | source |
queryAllByRole | Assert .toHaveLength(0) when the contract is no matching controls, including formerly multiple controls | Current query/disappearance docs | source; source |
waitForElementToBeRemoved | Pass a currently existing node or a query callback; await removal; initial null/empty array is an error | Current async API | source |
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 symptom | Cause | Fix | Source |
|---|---|---|---|
Found multiple elements with the role | The singular role query is ambiguous | Narrow accessible name or container; use queryAll only when multiple matches are part of the contract | source |
The element(s) given to waitForElementToBeRemoved are already removed. | Waiting begins with null, an empty list or an already removed node | Observe while present, or use waitFor with the absence assertion when removal may already have happened | source |
Missing
hidden: truebroadens 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