Skip to content

Comments

fix: propagate hidden flag to computeAccessibleName#1371

Open
foxaltus wants to merge 2 commits intotesting-library:mainfrom
foxaltus:propagate-hidden
Open

fix: propagate hidden flag to computeAccessibleName#1371
foxaltus wants to merge 2 commits intotesting-library:mainfrom
foxaltus:propagate-hidden

Conversation

@foxaltus
Copy link

What: this PR fixes #820 (comment)

Why: *ByRole APIs are slow, because the DOM environment relies on getComputedStyles to compute elements visibility. This can be mitigated by using the { hidden: true } option. However, when using *ByRole APIs along with the name option, the performance toll is still being paid because computeAccessibleName is called internally, which does not currently ignore hidden elements.

How: this PR propagates the hidden option to computeAccessibleName, which avoids paying the performance toll.

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • TypeScript definitions updated
  • Ready to be merged

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9bc828e:

Sandbox Source
react-testing-library-examples Configuration

@foxaltus
Copy link
Author

foxaltus commented Jan 15, 2026

Hmm I realize this is not enough...

The changes allow to bypass the expensive isHidden check at https://github.com/eps1lon/dom-accessibility-api/blob/main/sources/accessible-name-and-description.ts#L601, but not the ones at https://github.com/eps1lon/dom-accessibility-api/blob/main/sources/accessible-name-and-description.ts#L388. So the toll is still being paid :(

See: most time is being spent in computeMiscTextAlternative:
Screenshot of the flame graph showing most time being spent inside computeMiscTextAlternative
(FYI this test looks by table rows by name in a 25 rows x 10 cols table)

@github-actions
Copy link

Uh oh! @foxaltus, the image you shared is missing helpful alt text. Check #1371 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

@foxaltus
Copy link
Author

foxaltus commented Jan 16, 2026

At this point I think that maybe testing-library should offer an option to fake getComputedStyle (similar to the computedStyleSupportsPseudoElements option). In some of my tests (dealing with tables) this improves the performance by a factor of 10.

If this sounds too much of a bazooka solution, maybe we should instead consider adding an option at *ByRole level, such as namingStrategy?: 'fast' | 'accurate':

  • fast would send a mock getComputedStyle implementation to computeAccessibleName.
  • accurate (default) would use the current implementation (+ I think it could honour the hidden option, as already done in this PR)

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

waitFor + getByRole causing severe delays

1 participant