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

A more proper way to query form inline errors

Open
#634 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

References:
Describe the feature you'd like:

Until today, I've always used getByTestId to find a form inline error:

<form aria-label="form">
  <fieldset>
    <label for="email">Email</label>
    <input type="email" id="email" />
    <output data-testid="email-error" for="email">Email is required.</output>
  </fieldset>

  <fieldset>
    <label for="password">Password</label>
    <input type="password" id="password" />
    <output data-testid="password-error" for="password">Password is required.</output>
  </fieldset>

  <button type="submit">Submit</button>
</form>

But I'm always wondering if there is a more semantic / accessible way to both write the markup and find it in my tests.

In the html example I'm using the output tag with the for attribute (I'm still not very sure that its the proper use of this tag, lets discuss that 🤔), so maybe we could support a query like getByRole('status', {name: 'something'}) or another more getByLabelText style.

Suggested implementation:

I think that first we need to discuss the proper way to write an accessible inline form error and if possible to link it with its own input(s) (like a label).

Describe alternatives you've considered:

I've red in an article that a <div role="alert"> can be used, but is missing way to link it to the input(s), and AFAIK role="alert" is not the same as role="status" (the role from output tag).

Teachability, Documentation, Adoption, Migration Strategy:

Same as "Suggested Implementation"

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 reviewing the linked ARIA and accessibility references, then inspect the existing getByTestId, getByRole, and getByLabelText query behavior. The issue needs a decided accessible inline-error pattern and concrete query semantics before completion can be defined; no files or tests are identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
html, javascript
Domain
accessibility
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.