question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`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: Unable to find an accessible element with the role "listitem" and name

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: accessible names for li elements in the browser

Suggested solution:

Not really sure where the problem is located, to be honest.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Aug 11, 2021

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).

0reactions
silvenoncommented, Sep 18, 2021

Just wanted to say thanks for this great explanation @eps1lon! 🥇

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found