`ByRole` query gets empty accessible name for `li` elements
See original GitHub issue@testing-library/dom
version: 8.1.0- Testing Framework and version: jest v26.6.0
- DOM Environment: jsdom v26.6.2
- Node version: 14.15.4
Relevant code or config:
import { getByRole } from '@testing-library/dom';
function getExampleDOM() {
const root = document.createElement('div');
root.innerHTML = `
<ul>
<li>Item A</li>
<li>Item B</li>
</ul>
`;
return root;
}
test('should render list items', () => {
const container = getExampleDOM();
getByRole(container, 'list');
// the following query fails since it can't find a listitem element with provided accessible name
getByRole(container, 'listitem', { name: /item a/i });
});
What you did:
I’m trying to query a specific li
element using ByRole
query + accessible name.
What happened:
DOM Testing Library returns 0 elements matching the query since it can’t find any accessible name for the li
elements:
Reproduction:
I forked dom-testing-library-template
and reproduced the issue. You can find it here: https://github.com/Belco90/dom-testing-library-template/blob/main/src/__tests__/index.test.js
Problem description:
Correct me if I’m wrong, but li
child should be its accessible name if it is a static text:
Suggested solution:
Not really sure where the problem is located, to be honest.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
ByRole reads hidden element names as "" · Issue #846 - GitHub
The Testing Library error reporter claims the name of these elements is "" and fails to match them against the provided name:.
Read more >Unable to use getByRole on listitem with a specific name - RTL
For your specific query I'd suggest getAllByRole('listitem').find(listitem => listitem.textContent === 'Pending task') .
Read more >ByRole | Testing Library
You can query the returned element(s) by their accessible name or description. The accessible name is for simple cases equal to e.g. the ......
Read more >queryAllByRole function - rtl.dom.queries library - Dart API
Returns an empty list if no element(s) are found within the provided container . ... You can also query the returned element(s) by...
Read more >Common mistakes with React Testing Library - Kent C. Dodds
It comes from the same import statement you get render from: ... The name option allows you to query elements by their "Accessible...
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
Also see https://github.com/w3c/aria/issues/1117 where it’s explained in more detail why naming of listitem requires a lot of nuance (e.g. listitems containing lots of text).
Just wanted to say thanks for this great explanation @eps1lon! 🥇