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

`ByLabelText` does not ignore invalid IDREF in `aria-labelledby`

Open
#1,030 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

  • @testing-library/dom version: 8.5.0
  • Testing Framework and version: jest@26.6.3
  • DOM Environment: jsdom@16.5.3
Relevant code or config:
/**
 * @jest-environment jsdom
 */

const { getByRole, getByLabelText } = require('@testing-library/dom')

test('aria-labelledby', () => {
  const container = document.createElement('div')
  container.innerHTML = `
    <label>
      Name: <input aria-labelledby="oops">
    </label>
  `
  getByRole(container, 'textbox', { name: /Name/ })
  getByLabelText(container, /Name/)
})
What you did:
npx jest
What happened:
 FAIL  ./test.js
  ✕ aria-labelledby (64 ms)

  ● aria-labelledby

    TestingLibraryElementError: Found a label with the text of: /Name/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.

    Ignored nodes: comments, <script />, <style />
    <div>
      
        
      <label>
        
          Name: 
        <input
          aria-labelledby="oops"
        />
        
        
      </label>
      
      
    </div>

      13 |   `
      14 |   getByRole(container, 'textbox', { name: /Name/ })
    > 15 |   getByLabelText(container, /Name/)
         |   ^
      16 | })
      17 |
Reproduction:

silvenon/dom-testing-library-template

Problem description:

Perhaps I'm wrong, but based on what I read in the ByRole docs it seems that in this context the name option should behave the same way as ByLabelText.

Suggested solution:

In that case getByRole should also throw because aria-labelledby is wrong.

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 with the minimal test in test.js and run npx jest to compare getByRole and getByLabelText for the invalid aria-labelledby reference. Trace the two query entry points and add a regression test for the intended behavior, with the issue resolved when the queries handle the invalid IDREF consistently with the documented expectation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.