testing-library / testing-library/dom-testing-library

Using `getByLabelText` is also returning the hidden value of `fieldset` which is not a label.

Open
#1,253 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs more information
Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

  • @testing-library/dom version: 8.10.1
  • Testing Framework and version: Jest 29.6.3
  • DOM Environment: Brave Browser (current)
Relevant code or config:
expect(screen.getByLabelText('Access type')).toBeVisible();
What you did:

Testing to make sure a label is visible in the UI

What happened:

Multiple results were returned, including fieldset

 ● ClientOidcInfo › in readOnly mode › should have expected elements

    TestingLibraryElementError: Found multiple elements with the text of: Access type

    Here are the matching elements:

    <select
      aria-invalid="false"
      disabled=""
      id="access_type"
      name="access_type"
    >
      <option value="one">One</option>
      <option value="two">Two</option>
    </select>

    <div
      aria-label="Access type"
      role="textbox"
    >
      <label
        data-shrink="true"
        for="access_type"
        id="access_type-label"
      >
        Access type
      </label>
      <div>
        <select
          aria-invalid="false"
          disabled=""
          id="access_type"
          name="access_type"
        >
          <option value="one">One</option>
          <option value="two">Two</option>
        </select>
        <fieldset aria-hidden="true">
          <legend>
            <span>
              Access type
            </span>
          </legend>
        </fieldset>
      </div>
    </div>
Problem description:
  • fieldset is not part of the input label and shouldn't be included with getByLabelText
Suggested solution:

Do not return fieldset when using a query *ByLabelText

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported DOM with the getByLabelText query entry point and the shown aria-hidden fieldset. Inspect how matching labels and associated elements are collected, then add a regression test for this structure. Done means the query no longer returns the hidden fieldset while still finding the associated select.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.