-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-setup.ts
More file actions
104 lines (96 loc) · 3.04 KB
/
Copy pathtest-setup.ts
File metadata and controls
104 lines (96 loc) · 3.04 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Vitest global setup.
*
* - Registers @testing-library/jest-dom matchers (toBeInTheDocument, etc.)
* - Provides a thin axe-core wrapper via `expect(container).toBeAccessible()`
* (axe runs synchronously in Node; we expose a vitest-friendly matcher)
* - Polyfills matchMedia / ResizeObserver which jsdom does not implement
* but several Mitosis components touch (BcSplitter, BcAffix, BcBackTop).
*/
import '@testing-library/jest-dom/vitest';
import { afterEach, expect, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import axeCore from 'axe-core';
afterEach(() => {
cleanup();
});
// matchMedia polyfill (jsdom omits it). Components check
// `window.matchMedia('(prefers-reduced-motion: reduce)')`.
if (typeof window !== 'undefined' && !window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}),
});
}
// ResizeObserver polyfill — BcSplitter, BcAffix, BcBackTop use it.
if (typeof globalThis.ResizeObserver === 'undefined') {
class ResizeObserverStub {
observe() {}
unobserve() {}
disconnect() {}
}
(globalThis as unknown as { ResizeObserver: typeof ResizeObserverStub }).ResizeObserver =
ResizeObserverStub;
}
if (typeof HTMLCanvasElement !== 'undefined') {
Object.defineProperty(HTMLCanvasElement.prototype, 'getContext', {
writable: true,
value: vi.fn(() => null),
});
}
// IntersectionObserver polyfill — BcBackTop, BcAffix lazy-mount paths.
if (typeof globalThis.IntersectionObserver === 'undefined') {
class IntersectionObserverStub {
observe() {}
unobserve() {}
disconnect() {}
takeRecords() {
return [];
}
}
(globalThis as unknown as { IntersectionObserver: typeof IntersectionObserverStub }).IntersectionObserver =
IntersectionObserverStub;
}
// Custom vitest matcher: `expect(container).toBeAccessible()`
// Runs axe-core against the given container and fails on any violations.
expect.extend({
async toBeAccessible(received: HTMLElement) {
const results = await axeCore.run(received);
if (results.violations.length === 0) {
return {
pass: true,
message: () => 'expected element to have axe violations, but none were found',
};
}
const summary = results.violations
.map(
(v) =>
` [${v.id}] ${v.help}: ${v.nodes
.map((n) => n.html)
.slice(0, 3)
.join(', ')}`,
)
.join('\n');
return {
pass: false,
message: () =>
`expected element to be accessible, but axe found ${results.violations.length} violation(s):\n${summary}`,
};
},
});
interface CustomMatchers {
toBeAccessible(): Promise<void>;
}
declare module 'vitest' {
interface Assertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}