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 clickablediv, or an icon button with no label, fails the test.getByLabelTextfails when a label isn't linked to its input.
jest-dom adds matchers that check accessibility state:
toHaveAccessibleNamechecks the accessible name.toHaveAccessibleDescriptionchecks hints linked witharia-describedby.toBeInvalidchecksaria-invalid.toBeDisabledchecks the disabled state.toHaveFocuschecks 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,toBeInvalidandtoHaveFocus. - 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.
Read more
- ByRole (https://testing-library.com/docs/queries/byrole)testing-library.com
- Guiding principles (https://testing-library.com/docs/guiding-principles)testing-library.com