forked from bitbof/klecks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
96 lines (91 loc) · 3.08 KB
/
vitest.setup.ts
File metadata and controls
96 lines (91 loc) · 3.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
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
// vitest global setup for jsdom environment
// mock matchMedia for jsdom which doesn't support it
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => { },
removeListener: () => { },
addEventListener: () => { },
removeEventListener: () => { },
dispatchEvent: () => false,
}),
});
// mock requestAnimationFrame if not present
if (typeof window.requestAnimationFrame === 'undefined') {
window.requestAnimationFrame = (callback: FrameRequestCallback): number => {
return setTimeout(() => callback(Date.now()), 16) as unknown as number;
};
}
if (typeof window.cancelAnimationFrame === 'undefined') {
window.cancelAnimationFrame = (id: number): void => {
clearTimeout(id);
};
}
// mock performance.timing (deprecated but used)
if (window.performance && !window.performance.timing) {
(window.performance as any).timing = {
navigationStart: Date.now(),
};
}
// Global mock for canvas getContext to handle environments without 'canvas' package
(HTMLCanvasElement.prototype as any).getContext = function (type: string) {
if (type === '2d') {
const ctx = {
canvas: this,
save: () => { },
restore: () => { },
drawImage: () => { },
clearRect: () => { },
fillRect: () => { },
strokeRect: () => { },
fill: () => { },
stroke: () => { },
beginPath: () => { },
closePath: () => { },
moveTo: () => { },
lineTo: () => { },
rect: () => { },
arc: () => { },
setTransform: () => { },
translate: () => { },
scale: () => { },
rotate: () => { },
measureText: () => ({
width: 0,
actualBoundingBoxAscent: 0,
actualBoundingBoxDescent: 0,
actualBoundingBoxLeft: 0,
actualBoundingBoxRight: 0,
fontBoundingBoxAscent: 0,
fontBoundingBoxDescent: 0
}),
fillText: () => { },
strokeText: () => { },
createLinearGradient: () => ({ addColorStop: () => { } }),
createRadialGradient: () => ({ addColorStop: () => { } }),
createPattern: () => { },
createImageData: function (w: number, h: number) {
return {
data: new Uint8ClampedArray(w * h * 4),
width: w,
height: h,
colorSpace: 'srgb'
};
},
getImageData: function (x: number, y: number, w: number, h: number) {
return {
data: new Uint8ClampedArray(w * h * 4),
width: w,
height: h,
colorSpace: 'srgb'
};
},
putImageData: () => { },
};
return ctx;
}
return null;
};