Accessibility Testing with axe-core
Beginner10 minTrending
Audit React components for WCAG accessibility violations using axe-core in tests, CI, and browser DevTools.
Prerequisites
- -React project
- -Testing setup (Vitest or Jest)
Steps
1
Install axe-core testing utilities
Add jest-axe (or vitest-axe) to run accessibility checks in your test suite.
$ pnpm add -D axe-core vitest-axe
2
Write an accessibility test
Create a test that renders a component and checks for WCAG violations.
$ cat > src/components/__tests__/Form.a11y.test.tsx << 'EOF'
import { render } from '@testing-library/react';
import { axe, toHaveNoViolations } from 'vitest-axe';
import { expect } from 'vitest';
import { ContactForm } from '../ContactForm';
expect.extend(toHaveNoViolations);
it('ContactForm has no accessibility violations', async () => {
const { container } = render(<ContactForm />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
EOF
Run axe checks on every component to catch missing labels, low contrast, and incorrect ARIA attributes.
3
Add eslint-plugin-jsx-a11y for static analysis
Catch accessibility issues at lint time before they reach tests or production.
$ pnpm add -D eslint-plugin-jsx-a11y
4
Run a full-page accessibility audit with Lighthouse
Use Lighthouse specifically for the accessibility category.
$ npx lighthouse http://localhost:3000 --only-categories=accessibility --output=html --output-path=./a11y-report.html --chrome-flags='--headless=new'
Full Script
FAQ
Discussion
Loading comments...