-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
86 lines (77 loc) · 1.96 KB
/
vitest.setup.ts
File metadata and controls
86 lines (77 loc) · 1.96 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
import "@testing-library/jest-dom"
import { vi } from "vitest"
// Polyfill ResizeObserver for tests
global.ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
// Mock window.matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
readonly root: Element | null = null
readonly rootMargin: string = ""
readonly thresholds: ReadonlyArray<number> = []
constructor() {}
observe() {}
unobserve() {}
disconnect() {}
takeRecords(): IntersectionObserverEntry[] {
return []
}
}
// Mock document.elementFromPoint
if (typeof document !== "undefined") {
document.elementFromPoint = vi.fn().mockReturnValue(null)
}
// Mock scrollTo
window.scrollTo = vi.fn()
// Mock crypto.getRandomValues
Object.defineProperty(global, "crypto", {
value: {
getRandomValues: (arr: Uint8Array) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256)
}
return arr
},
randomUUID: () => {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
const r = (Math.random() * 16) | 0
const v = c === "x" ? r : (r & 0x3) | 0x8
return v.toString(16)
}
)
},
},
})
// Mock fetch
global.fetch = vi.fn()
// Suppress console errors in tests (optional)
const originalError = console.error
console.error = (...args: unknown[]) => {
// Filter out React 18 act() warnings
const firstArg = args[0]
if (
typeof firstArg === "string" &&
firstArg.includes("Warning: An update to")
) {
return
}
originalError.apply(console, args)
}