Repository navigation
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
99 lines (83 loc) · 2.83 KB
/
Copy pathvitest.setup.ts
File metadata and controls
99 lines (83 loc) · 2.83 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 '@testing-library/jest-dom';
// Custom Storage prototype override to fix Node.js v25+ experimental localStorage incompatibility with JSDOM
if (typeof window !== 'undefined' && typeof window.Storage !== 'undefined') {
const stores = new WeakMap<object, Map<string, string>>();
const getStore = (instance: object) => {
let store = stores.get(instance);
if (!store) {
store = new Map<string, string>();
stores.set(instance, store);
}
return store;
};
Object.defineProperty(window.Storage.prototype, 'length', {
get() {
return getStore(this).size;
},
configurable: true,
});
window.Storage.prototype.getItem = function (key: string) {
return getStore(this).get(key) ?? null;
};
window.Storage.prototype.setItem = function (key: string, value: string) {
getStore(this).set(key, String(value));
};
window.Storage.prototype.removeItem = function (key: string) {
getStore(this).delete(key);
};
window.Storage.prototype.clear = function () {
getStore(this).clear();
};
window.Storage.prototype.key = function (index: number) {
return Array.from(getStore(this).keys())[index] ?? null;
};
// Re-create localStorage and sessionStorage to be genuine Storage instances
const mockLocalStorage = Object.create(window.Storage.prototype);
const mockSessionStorage = Object.create(window.Storage.prototype);
Object.defineProperty(window, 'localStorage', {
value: mockLocalStorage,
writable: true,
configurable: true,
});
Object.defineProperty(window, 'sessionStorage', {
value: mockSessionStorage,
writable: true,
configurable: true,
});
Object.defineProperty(globalThis, 'localStorage', {
value: mockLocalStorage,
writable: true,
configurable: true,
});
Object.defineProperty(globalThis, 'sessionStorage', {
value: mockSessionStorage,
writable: true,
configurable: true,
});
}
if (typeof globalThis.fetch !== 'undefined') {
const originalFetch = globalThis.fetch;
globalThis.fetch = function (url: URL | RequestInfo, init?: RequestInit) {
const urlString =
typeof url === 'string'
? url
: url instanceof URL
? url.toString()
: url && typeof url === 'object' && 'url' in url
? (url as Request).url
: '';
// Allow localhost/127.0.0.1 and data: URLs (inline resources/WebAssembly)
const normalizedUrl = urlString.trim().toLowerCase();
if (
normalizedUrl.includes('localhost') ||
normalizedUrl.includes('127.0.0.1') ||
normalizedUrl.startsWith('data:')
) {
return originalFetch(url, init);
}
throw new Error(
`[Vitest Guard] Blocked outbound network request to: ${urlString}. ` +
`Do not make real network requests in unit tests. Please mock global.fetch or use MSW.`
);
} as typeof fetch;
}