-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-setup.ts
More file actions
38 lines (35 loc) · 1005 Bytes
/
Copy pathtest-setup.ts
File metadata and controls
38 lines (35 loc) · 1005 Bytes
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
import '@testing-library/jest-dom/vitest';
// jsdom in this environment does not expose window.localStorage; give tests
// a minimal in-memory implementation so components touching it can mount.
function ensureLocalStorage() {
try {
if (window.localStorage) {
return;
}
} catch {
// Opaque-origin jsdom throws on access; replace it below.
}
const store = new Map<string, string>();
Object.defineProperty(window, 'localStorage', {
configurable: true,
value: {
getItem: (key: string) => store.get(String(key)) ?? null,
setItem: (key: string, value: string) => {
store.set(String(key), String(value));
},
removeItem: (key: string) => {
store.delete(String(key));
},
clear: () => {
store.clear();
},
key: (index: number) => Array.from(store.keys())[index] ?? null,
get length() {
return store.size;
}
}
});
}
if (typeof window !== 'undefined') {
ensureLocalStorage();
}