Skip to content

Unable to getByRole figure with accessible name / description in figcaption #1417

Open

Description

@Mboulianne
  • @testing-library/react version: 16.3.0
  • Testing Framework and version: jest 30.0.5
  • DOM Environment: jest-environment-jsdom 30.0.5

Relevant code or config:

<figure>
  <figcaption>Foo</figcaption>
  <ul>
    <li>Bar</li>
  </ul>
</figure>

What you did:

What trying to get hold of the figure element using:
const figure = screen.getByRole("figure", { name: "Foo" });
or
const figure = screen.getByRole("figure", { description: "Foo" });
or
const figure = screen.getByRole("figure", { name: /Foo/ });
or
const figure = screen.getByRole("figure", { description: /Foo/ });

What happened:

ShadowDOMTestingLibraryElementError: Unable to find an element with the role of: figure

Reproduction:

Problem description:

Unable to get figure using accessible name / description in figcaption

As per:
https://www.w3.org/WAI/ARIA/apg/practices/names-and-descriptions/

Like with table elements, if a figure is not named using aria-label or aria-labelledby, the content of the figcaption element will be used as the accessible name. However unlike table elements, if the figcaption element is not used for the name, it does not become an accessible description unless it is referenced by aria-describedby. Nevertheless, assistive technologies will render the content of a figcaption regardless of whether it is used as a name, description, or neither.

Activity

  1. karesztrk commented on Jun 1, 2026

    @karesztrk

    According to Adrian Roselli <figcaption> shouldn't give accessible name. But MDN says it the other way around:

    The figcaption HTML element represents a caption or legend describing the rest of the contents of its parent

    element, providing the an accessible name.

  2. Huaian666 commented on Jun 6, 2026

    @Huaian666

    finally something that doesn't try to do everything. does one thing, does it well, gets out of your way. respect.

  3. Huaian666 commented on Jun 6, 2026

    @Huaian666

    been using this at work for a few weeks. handles edge cases that caught us off guard with other tools. really well thought out.

  4. beanscg commented on Jun 9, 2026

    @beanscg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions