-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
80 lines (71 loc) · 2.18 KB
/
vitest.setup.ts
File metadata and controls
80 lines (71 loc) · 2.18 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
import '@testing-library/jest-dom/vitest';
import * as vitestAxeMatchers from 'vitest-axe/matchers';
expect.extend(vitestAxeMatchers);
process.env.TZ = 'UTC';
Object.defineProperty(globalThis, 'crypto', {
value: {
getRandomValues: (arr: Uint8Array) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256);
}
return arr;
},
},
});
Object.defineProperty(globalThis, 'structuredClone', {
value: (val: unknown) => JSON.parse(JSON.stringify(val)),
});
// Mock proper File API behavior
if (typeof File === 'undefined') {
class FilePoly extends Blob {
name: string;
lastModified: number;
constructor(bits: BlobPart[], name: string, options: FilePropertyBag = {}) {
super(bits, options);
this.name = name;
this.lastModified = options.lastModified || Date.now();
}
}
// @ts-expect-error polyfill
globalThis.File = FilePoly;
}
// Ensure FileReader is available
if (typeof FileReader === 'undefined') {
class FileReaderPoly {
result: string | null = null;
onload: (() => void) | null = null;
readAsDataURL(blob: Blob) {
const result = `data:${blob.type};base64,${Buffer.from(blob as unknown as ArrayBuffer).toString('base64')}`;
setTimeout(() => {
this.result = result;
this.onload?.();
}, 0);
}
}
// @ts-expect-error polyfill
globalThis.FileReader = FileReaderPoly;
}
// Mock URL.createObjectURL
if (typeof URL.createObjectURL === 'undefined') {
URL.createObjectURL = (file: Blob) => `blob:${(file as File).name}`;
}
if (typeof URL.revokeObjectURL === 'undefined') {
URL.revokeObjectURL = () => {};
}
if (typeof window !== 'undefined' && typeof window.matchMedia === 'undefined') {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
addEventListener: () => null,
addListener: () => null,
dispatchEvent: () => false,
matches: false,
media: query,
onchange: null,
removeEventListener: () => null,
removeListener: () => null,
}),
});
}
// Mock HTMLCanvasElement.getContext for vitest-axe/axe-core
HTMLCanvasElement.prototype.getContext = (() => null) as any;