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

How can your component tests also help catch accessibility problems?

Querying by role and accessible name already checks a lot of accessibility. The accessible name is what a screen reader reads out for an element.

What queries catch:

  • getByRole('button', { name: /save/i }) only succeeds if the element really has the button role and a name. So a clickable div, or an icon button with no label, fails the test.
  • getByLabelText fails when a label isn't linked to its input.

jest-dom adds matchers that check accessibility state:

  • toHaveAccessibleName checks the accessible name.
  • toHaveAccessibleDescription checks hints linked with aria-describedby.
  • toBeInvalid checks aria-invalid.
  • toBeDisabled checks the disabled state.
  • toHaveFocus checks focus management.

Using user-event's keyboard API tests that the UI works without a mouse.

You can also run automated checkers such as axe in tests. But they find only part of the issues. Real assistive-technology checks and E2E tests still matter.

  • Role and name queries fail on inaccessible markup.
  • Label queries catch labels that aren't linked to their inputs.
  • jest-dom has accessibility matchers like toHaveAccessibleName, toBeInvalid and toHaveFocus.
  • Keyboard tests with user-event check that the UI works without a mouse.
  • Automated checkers like axe help, but they don't replace manual checks.