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.

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:

image

Reproduction:

References:

This was originally discovered at Discord KCD by Thomas.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
thomasmerycommented, Dec 13, 2021

thanks!

test is passing now

I had to upgrade to:

   @testing-library/dom: ^8.11.1 => 8.11.1 
   @testing-library/jest-dom: ^5.16.1 => 5.16.1 
   @testing-library/react: ^12.1.2 => 12.1.2
   dom-accessibility-api: ^0.5.10 => 0.5.10 
1reaction
eps1loncommented, Dec 13, 2021

@thomasmery Could you post the result of npx testing-library-envinfo react? You might have an older version of dom-accessibility-api that was fixed in https://github.com/testing-library/dom-testing-library/issues/1059

Read more comments on GitHub >

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

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