testing-library / testing-library/dom-testing-library

GetByRole does not pickup accessibility name correctly from aria-labelledby with hidden children

Open
#1,372 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

  • @testing-library/dom version: 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.