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's error message is incorrect on button with role "tab"

See original GitHub issue
  • DOM Testing Library version: 5.5.0
  • node version: 13.12.0
  • npm (or yarn) version: 1.22.4

Relevant code or config:

import React from 'react';
import { render, screen } from '@testing-library/react';

const Button = () => <button role="tab" aria-label="my-button" />;

test('button exists', () => {
  render(<Button />);

  screen.getByRole('button', { name: 'my-button' });
});

What happened:

The test (correctly) failed because it could not find an element with the role of button (the button has a role of “tab”). However, when the error message listed the accessible roles, it listed the button’s role under “button”:

TestingLibraryElementError: Unable to find an accessible element with the role "button" and name "my-button"

Here are the accessible roles:

  document:

  Name "":
  <body />

  --------------------------------------------------
  button:

  Name "my-button":
  <button
    aria-label="my-button"
    role="tab"
  />

Reproduction:

Code Sandbox

Problem description:

The current behaviour is confusing because the error message says there is no element with the role of “button” and then shows that there is an element with the role of “button”.

Suggested solution:

Buttons with other roles (e.g. tab) should be shown under their correct roles in the error message.

I’m happy to put up a PR for this but would need pointing in the right direction as I’m unfamiliar with the code 😁

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, May 13, 2020

🎉 This issue has been resolved in version 7.5.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
mattstobbscommented, May 8, 2020

It looks like there are actually 2 issues here.

The first is the library aria-query, which we’re using to get a map of the elements to their possible roles. Under elementRoles, the button element only has the role “button”. This would need to be extended to include other roles, such as tab.

The second is that the matches function correctly determines that <button role="tab /> has the role of tab, but currentNode.matches determines the node has the role of button. Do you know where that function is defined?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant find button using Test Library (React)
In this case, you get this very misleading error message: Unable to find role="button". I just lost half an hour on a simple...
Read more >
ByRole
Default roles are taken into consideration e.g. <button /> has the button role without explicitly setting the role attribute.
Read more >
ARIA in HTML
Specifying this role is unnecessary, as a "button" element is already exposed with an implicit button role. In practice this particular instance ...
Read more >
An in-depth beginner's guide to testing React applications ...
Find out what to test and which testing framework to use. Learn a clear approach by writing tests for an extensive example app...
Read more >
Testing | RedwoodJS Docs
Switch to the Javascript tab below to see the code: ... Raising an error passes the test, not raising the error (or raising...
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