Skip to content

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

Open
@CWSites

Description

@CWSites
  • @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions