-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
40 lines (35 loc) · 1.08 KB
/
vitest.setup.ts
File metadata and controls
40 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { type AxeResults } from 'axe-core';
import { expect } from 'vitest';
import type { ExpectationResult } from '@vitest/expect';
/**
* Custom Vitest matcher: expect(await axeRun(el)).toHaveNoViolations()
*/
expect.extend({
toHaveNoViolations(results: AxeResults): ExpectationResult {
const violations = results.violations;
if (violations.length === 0) {
return {
pass: true,
message: () => 'Expected accessibility violations but found none',
};
}
const messages = violations.map(v => {
const nodes = v.nodes.map(n => ` - ${n.html}`).join('\n');
return `${v.id} (${v.impact}): ${v.description}\n${nodes}`;
});
return {
pass: false,
message: () =>
`Expected no accessibility violations but found ${violations.length}:\n\n${messages.join('\n\n')}`,
};
},
});
declare module 'vitest' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Assertion<T = any> {
toHaveNoViolations(): void;
}
interface AsymmetricMatchersContaining {
toHaveNoViolations(): void;
}
}