forked from JhaSourav07/commitpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
70 lines (57 loc) · 1.9 KB
/
vitest.setup.ts
File metadata and controls
70 lines (57 loc) · 1.9 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
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,
});
}