getByRole() doesn't recognize header's implicit banner role
See original GitHub issue@testing-library/dom
version:7.5.1
@testing-library/jest-dom
:5.7.0
@testing-library/react
:10.0.4
jest
:24.9.0
Relevant code or config:
import React from "react";
import { render, screen } from "@testing-library/react";
describe("getByRole", () => {
test("doens't consider a <header> as banner role", () => {
render(<header>Test</header>);
expect(screen.getByRole("banner")).toBeInTheDocument();
});
});
What you did:
The header
tag has an implicit banner
role. RTL’s getByRole
should be able to identify this.
What happened:
TestingLibraryElementError: Unable to find an accessible element with the role "banner"
There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
<body>
<div>
<header>
Test
</header>
</div>
</body>
Problem description:
From the official W3C specs:
- The HTML
header
element defines abanner
landmark when its context is thebody
element. - The HTML
header
element is not considered abanner
landmark when it is descendant of any of following elements:
article
aside
main
nav
section
Chrome gets this one right:
Suggested solution:
RTL should be able to identify the role correctly with respect to the above banner
specs for the header
tag.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
ByRole | Testing Library
An element with the heading role can be queried by any heading level getByRole('heading') or by a specific heading level using the level...
Read more >ARIA: presentation role - Accessibility - MDN Web Docs - Mozilla
The presentation role and its synonym none remove an element's implicit ARIA semantics from being exposed to the accessibility tree.
Read more >Using the React Testing Library debug method - LogRocket Blog
test('should view implicit roles with logRoles', async () => { render(<Posts />); const postItemNode = await screen.findByRole('heading', ...
Read more >No suggestions on getByRole, queryByRole, etc
debug() I do see the entire component. So that might be? That the button from bootstrap won't work with the getByRole query. –...
Read more >React Testing Guide - Components.Guide
Most semantic HTML elements have an implicit role. ... So using them does not give us that much more confidence that what we...
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.16.3 🎉
The release is available on:
npm package (@latest dist-tag)
Your semantic-release bot 📦🚀
I’ve updated
aria-query
to the latest version, but the issue still occurs, unfortunately. I’ve provided a PR.