-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
99 lines (86 loc) · 2.91 KB
/
vitest.setup.ts
File metadata and controls
99 lines (86 loc) · 2.91 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
import { TextEncoder, TextDecoder } from 'util';
import { cleanup } from '@testing-library/react';
import { afterAll, afterEach, beforeAll, beforeEach, vi } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { loadDevMessages, loadErrorMessages } from '@apollo/client/dev';
loadDevMessages();
loadErrorMessages();
import { setupLocalStorageMock } from './src/test-utils/localStorageMock';
// Setup localStorage mock globally for all tests
const localStorageMock = setupLocalStorageMock();
if (typeof globalThis.localStorage === 'undefined') {
globalThis.localStorage = localStorageMock as unknown as Storage;
}
// Simple console error handler for React 18 warnings
const originalError = console.error;
const originalWarn = console.warn;
const shouldSuppressError = (value: unknown): boolean => {
if (typeof value !== 'string') {
if (value instanceof Error) {
return shouldSuppressError(value.message);
}
return false;
}
return value.includes(
'Warning: ReactDOM.render is no longer supported in React 18.',
);
};
vi.stubGlobal('localStorage', localStorageMock);
beforeAll(() => {
console.error = (...args: unknown[]) => {
if (args.some(shouldSuppressError)) {
return; // Suppress known React 18 warnings
}
originalError.call(console, ...args);
};
console.warn = (...args: unknown[]) => {
if (args.some(shouldSuppressError)) {
return;
}
originalWarn.call(console, ...args);
};
});
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
get: () => localStorageMock as unknown as Storage,
set: () => {
// swallow attempts to overwrite window.localStorage from tests
},
});
// Basic cleanup before each test
beforeEach(() => {
const g = globalThis as unknown as { localStorage: unknown };
if (g.localStorage !== (localStorageMock as unknown as Storage)) {
vi.stubGlobal('localStorage', localStorageMock as unknown as Storage);
}
});
// Basic cleanup after each test
afterEach(() => {
cleanup();
vi.clearAllMocks();
localStorageMock.clear();
});
// Global mocks for URL API (needed for file upload tests)
// TODO: Remove once test isolation is properly fixed in individual test files
global.URL.createObjectURL = vi.fn(() => 'mock-object-url');
global.URL.revokeObjectURL = vi.fn();
// Mock HTMLFormElement.prototype.requestSubmit for jsdom
// TODO: Remove once jsdom adds native support
if (typeof HTMLFormElement.prototype.requestSubmit === 'undefined') {
HTMLFormElement.prototype.requestSubmit = function () {
if (this.checkValidity()) {
this.dispatchEvent(
new Event('submit', { cancelable: true, bubbles: true }),
);
}
};
}
afterAll(() => {
console.error = originalError;
console.warn = originalWarn;
});
// Polyfill for @pdfme
if (typeof global.TextEncoder === 'undefined') {
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as unknown as typeof global.TextDecoder;
}