Skip to content

getByRole() doesn't recognize header's implicit banner role #578

Closed
@darekkay

Description

@darekkay
  • @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:

  1. The HTML header element defines a banner landmark when its context is the body element.
  2. The HTML header element is not considered a banner landmark when it is descendant of any of following elements:
  • article
  • aside
  • main
  • nav
  • section

Chrome gets this one right:

image

Suggested solution:

RTL should be able to identify the role correctly with respect to the above banner specs for the header tag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions