Skip to content

vitest-axe

Axe matchers for Vitest.

Install

sh
npm i -D axe-core @chialab/vitest-axe
sh
yarn add -D axe-core @chialab/vitest-axe
sh
pnpm add -D axe-core @chialab/vitest-axe

Usage

Use a Vitest setup file to add the matchers to the test runner expectation API.
Also, make sure to include typings for the matchers in the tsconfig.json file.

ts
export default {
    test: {
        setupFiles: ['./test/setup.ts'],
    },
}
ts
import matchers from '@chialab/vitest-axe';
import { expect } from 'vitest';

expect.extend(matchers);
json
{
    "compilerOptions": {
        // ...
        "types": ["@chialab/vitest-axe/matchers"]
    }
}

Example

ts
import { run as axe } from 'axe-core';
import { describe, expect, test } from 'vitest';

describe('button', () => {
    test('accessibility', async () => {
        const button = document.createElement('button');
        expect(await axe(button)).toHaveNoViolations();
    });
});

Released under the MIT License.