CodePark/ questions / Forms & accessibility
START PRACTICINGPRACTICE
Frontend question · Testing › Forms & accessibility

Why does screen.getByLabelText('Email') throw for this markup?

<label>Email</label>
<input type="email" name="email" />
The label isn't associated with the input; it needs htmlFor matching the input's id, or must wrap the input

The label isn't linked to the input. A label sitting next to an input isn't connected to it, so screen readers don't announce it either. The failing test is pointing at a real accessibility bug. Link them with htmlFor and id, wrap the input in the label, or use aria-labelledby. The tempting aria-label option is wrong. getByLabelText also finds inputs through <label> elements and aria-labelledby.