getByRole's error message is incorrect on button with role "tab"
See original GitHub issueDOM Testing Library
version: 5.5.0node
version: 13.12.0npm
(oryarn
) 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:
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:
- Created 3 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top 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 >
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
🎉 This issue has been resolved in version 7.5.3 🎉
The release is available on:
npm package (@latest dist-tag)
Your semantic-release bot 📦🚀
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, butcurrentNode.matches
determines the node has the role of button. Do you know where that function is defined?