-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
72 lines (67 loc) · 1.6 KB
/
vitest.setup.ts
File metadata and controls
72 lines (67 loc) · 1.6 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
const storageState = new Map<string, string>();
const storageProto = Storage.prototype;
Object.defineProperties(storageProto, {
clear: {
configurable: true,
enumerable: false,
value: function clear() {
storageState.clear();
},
writable: true,
},
getItem: {
configurable: true,
enumerable: false,
value: function getItem(key: string) {
const normalizedKey = String(key);
return storageState.has(normalizedKey) ? storageState.get(normalizedKey) ?? null : null;
},
writable: true,
},
key: {
configurable: true,
enumerable: false,
value: function key(index: number) {
return Array.from(storageState.keys())[index] ?? null;
},
writable: true,
},
length: {
configurable: true,
enumerable: true,
get() {
return storageState.size;
},
},
removeItem: {
configurable: true,
enumerable: false,
value: function removeItem(key: string) {
storageState.delete(String(key));
},
writable: true,
},
setItem: {
configurable: true,
enumerable: false,
value: function setItem(key: string, value: string) {
storageState.set(String(key), String(value));
},
writable: true,
},
});
const localStorageMock = Object.create(storageProto) as Storage;
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
enumerable: true,
value: localStorageMock,
writable: true,
});
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'localStorage', {
configurable: true,
enumerable: true,
value: localStorageMock,
writable: true,
});
}