-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathglobal.setup.ts
More file actions
74 lines (66 loc) · 1.7 KB
/
global.setup.ts
File metadata and controls
74 lines (66 loc) · 1.7 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
/**
* Global setup utilities that run once before all tests.
*
* Use this for expensive setup that should only happen once.
*/
import { vi } from 'vitest'
/**
* Suppresses specific console warnings/errors during tests.
*/
export function suppressConsoleWarnings(patterns: RegExp[]): void {
const originalWarn = console.warn
const originalError = console.error
console.warn = (...args: any[]) => {
const message = args.join(' ')
if (patterns.some((pattern) => pattern.test(message))) {
return
}
originalWarn.apply(console, args)
}
console.error = (...args: any[]) => {
const message = args.join(' ')
if (patterns.some((pattern) => pattern.test(message))) {
return
}
originalError.apply(console, args)
}
}
/**
* Common patterns to suppress in tests.
*/
export const COMMON_SUPPRESS_PATTERNS = [
/Zustand.*persist middleware/i,
/React does not recognize the.*prop/,
/Warning: Invalid DOM property/,
/act\(\) warning/,
]
/**
* Sets up global mocks for Node.js environment.
*/
export function setupNodeEnvironment(): void {
// Mock window if not present
if (typeof window === 'undefined') {
vi.stubGlobal('window', {
location: { href: 'http://localhost:3000' },
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
})
}
// Mock document if not present
if (typeof document === 'undefined') {
vi.stubGlobal('document', {
createElement: vi.fn(() => ({
style: {},
setAttribute: vi.fn(),
appendChild: vi.fn(),
})),
body: { appendChild: vi.fn() },
})
}
}
/**
* Cleans up global mocks after tests.
*/
export function cleanupGlobalMocks(): void {
vi.unstubAllGlobals()
}