-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
103 lines (89 loc) · 2.58 KB
/
vitest.setup.ts
File metadata and controls
103 lines (89 loc) · 2.58 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
import '@testing-library/jest-dom'
// Global test setup
// This file runs before each test file
// Mock DOM APIs that might not be available in jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock ResizeObserver
(globalThis as any).ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// Mock IntersectionObserver
(globalThis as any).IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
// Mock requestAnimationFrame
(globalThis as any).requestAnimationFrame = vi.fn().mockImplementation((cb: Function) => {
return setTimeout(cb, 0);
});
(globalThis as any).cancelAnimationFrame = vi.fn().mockImplementation((id: number) => {
clearTimeout(id);
});
// Mock performance.now for timing-related tests
Object.defineProperty(window, 'performance', {
writable: true,
value: {
now: vi.fn(() => Date.now())
}
});
// Mock CSS properties that might be used by gridstack
Object.defineProperty(window, 'getComputedStyle', {
value: () => ({
getPropertyValue: () => '',
width: '100px',
height: '100px',
marginTop: '0px',
marginBottom: '0px',
marginLeft: '0px',
marginRight: '0px'
})
});
// Mock scrollTo for tests that might trigger scrolling
window.scrollTo = vi.fn();
// Setup DOM environment
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost:3000',
origin: 'http://localhost:3000',
pathname: '/',
search: '',
hash: ''
},
writable: true
});
// Global test utilities
(globalThis as any).createMockElement = (tagName: string = 'div', attributes: Record<string, string> = {}) => {
const element = document.createElement(tagName);
Object.entries(attributes).forEach(([key, value]) => {
element.setAttribute(key, value);
});
return element;
};
// Console error/warning suppression for expected errors in tests
const originalError = console.error;
const originalWarn = console.warn;
beforeEach(() => {
// Reset console methods for each test
console.error = originalError;
console.warn = originalWarn;
});
// Helper to suppress expected console errors/warnings
(globalThis as any).suppressConsoleErrors = () => {
console.error = vi.fn();
console.warn = vi.fn();
};