generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
78 lines (69 loc) · 2.11 KB
/
vitest.setup.ts
File metadata and controls
78 lines (69 loc) · 2.11 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
import { afterEach, vi, beforeAll } from 'vitest';
// Track unhandled errors for better debugging
const originalConsoleError = console.error;
console.error = (...args) => {
// Check if this is an unhandled error we want to track
if (args.some(arg => typeof arg === 'object' && arg !== null)) {
// Convert objects to string for better error reporting
const stringifiedArgs = args.map((arg: unknown) =>
typeof arg === 'object' && arg !== null
? JSON.stringify(arg, null, 2)
: arg
);
originalConsoleError.apply(console, stringifiedArgs);
} else {
originalConsoleError.apply(console, args);
}
};
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Promise Rejection at:', promise, 'reason:', reason);
});
// Handle uncaught exceptions
process.on('uncaughtException', error => {
console.error('Uncaught Exception:', error);
});
// Mock Astro content collections before any imports
beforeAll(() => {
// Mock astro:content module
vi.mock('astro:content', () => ({
getEntry: vi.fn().mockResolvedValue({
data: {
'nav.home': 'Home',
'nav.docs': 'Docs',
'getting-started': 'Getting Started',
FAQ: 'FAQ'
}
}),
getCollection: vi.fn().mockResolvedValue([])
}));
});
// Global cleanup after each test
afterEach(() => {
// Clear all timers
vi.clearAllTimers();
// Clear all mocks except the persistent Astro mocks
vi.clearAllMocks();
// Reset DOM if needed
if (typeof document !== 'undefined') {
document.body.innerHTML = '';
document.head.innerHTML = '';
}
// Clear JSDOM instances properly
if (typeof window !== 'undefined' && window.close) {
try {
window.close();
} catch {
// Ignore errors during window cleanup
}
}
// Clear global window modifications
if (typeof global !== 'undefined') {
const globalObj = global as Record<string, unknown>;
delete globalObj.window;
delete globalObj.document;
delete globalObj.HTMLElement;
delete globalObj.customElements;
delete globalObj.localStorage;
}
});