testing-library / testing-library/dom-testing-library
GetByRole does not pickup accessibility name correctly from aria-labelledby with hidden children
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 3.3k
- Forks
- 474
- PR merge metrics
- No merged PRs in 30d
Description
@testing-library/domversion: 10.0.0- Testing Framework and version: vitest 0.34.6
- DOM Environment: Brave: 1.86.39 / Chromium: 144.0.7559.59
Relevant code or config:
Two examples of code failing:
<div>
<button type="button" aria-labelledby="tooltip"><i class="fa-solid fa-file"></i></button>
<span id="tooltip" role="tooltip" aria-hidden="true">
<span hidden>Hidden</span>
</span>
</div>
<div>
<button type="button" aria-labelledby="tooltip"><i class="fa-solid fa-file"></i></button>
<span id="tooltip" role="tooltip" aria-hidden="true">
<span style="display: none">Hidden</span>
</span>
</div>
What you did:
I was trying to debug why findByRole in Cypress testing library isn't getting a button by Carbon React. While trying to isolate the issue, I noticed that the browser will pick up the accessibility name but the dom-testing-library does not.
Surprisingly, there is a workaround for this in the form of just hiding the items via css as shown below
<div>
<button type="button" aria-labelledby="tooltip"><i class="fa-solid fa-file"></i></button>
<span id="tooltip" role="tooltip" aria-hidden="true">
<span class="display-none">Hidden</span>
</span>
</div>
.display-none {
display: none
}
What happened:
I am expecting the test to pass but the test has failed
Reproduction:
https://stackblitz.com/edit/dtl-template-5rtk3g2m?file=src%2Fmain.test.ts
Problem description:
The testing library behavior does not reflect the behavior in the browser.
Suggested solution:
Look into getByRole implementation to reproduce and find a fix.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the linked StackBlitz reproduction first, then inspect the getByRole implementation and how it derives names from aria-labelledby references containing hidden children. Done when the library matches the browser's accessible name behavior and the reproduction passes for both hidden and display:none cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- accessibility, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100