getByRole doesn't work for menu with aria-label
See original GitHub issue@testing-library/dom
version: 4.1.0- Testing Framework and version: Jest (The same as in the codesandbox below)
Relevant code or config:
<ul role="menu" aria-label="actions">
<li role="menuitem">copy</li>
<li role="menuitem">paste</li>
<li role="menuitem">cut</li>
</ul>
<ul role="menu" aria-label="share">
<li role="menuitem">copy link</li>
<li role="menuitem">report bug</li>
</ul>
What you did:
screen.getByRole('menu', { name: 'actions' })
What happened:
The element was not found.
Unable to find an accessible element with the role “menu” and name “actions”
I was expecting to be found because, according to the Chrome’s Accessibility Tree, the role and name are set correctly:
Reproduction:
- Condesabox: https://codesandbox.io/s/react-testing-library-menu-name-6ir07
- Testing Playground: https://testing-playground.com/gist/ba36265b023b1e0bb5dbe513cceb84f1/d07dc90240b88c2c71ad7c1478a7e40ba800778d
References:
This was originally discovered at Discord KCD by Thomas.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Why doesn't getByRole get an element with a name (but it ...
If I replace name={'day'} with aria-label={'day'} in the component, the button can be found by screen.getByRole('button', { name: 'day' }) . Why ...
Read more >ByRole | Testing Library
The accessible name is for simple cases equal to e.g. the label of a form element, or the text content of a button,...
Read more >Unable to find an accessible element with the role "form" #474
getByRole ("form") however it doesn't work when doing screen. ... It's not user-accessible content like a label tag or aria-label .
Read more >getByRole function - rtl.dom.queries library - Dart API - Pub.dev
The accessible name is for simple cases equal to the label of a form element, or the text content of a button, or...
Read more >Testing Lists Items With React Testing Library | Balavishnu V J
We are able to query the the list by screen.getByRole("list", { name: /fruits/i }) because we have labelled the <ul> with aria-label ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
thanks!
test is passing now
I had to upgrade to:
@thomasmery Could you post the result of
npx testing-library-envinfo react
? You might have an older version ofdom-accessibility-api
that was fixed in https://github.com/testing-library/dom-testing-library/issues/1059