-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-setup.js
More file actions
76 lines (71 loc) · 2.28 KB
/
Copy pathtest-setup.js
File metadata and controls
76 lines (71 loc) · 2.28 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
/**
* test-setup.js — vitest setup hook (loaded before each test file).
*
* Node 22+ ships a built-in `localStorage` global that is a stub `{}`
* unless invoked with `--localstorage-file=<path>`. Node 25.x exposes
* this even without the flag, which shadows jsdom's `window.localStorage`
* — `localStorage.setItem(...)` then crashes with `TypeError: ... is
* not a function`. jsdom does not override the existing global.
*
* Workaround: install a tiny in-memory Storage implementation onto both
* `globalThis.localStorage` and (when jsdom is the env) `window.localStorage`.
* Same object reference for both so `globalThis.localStorage ===
* window.localStorage` holds, matching the contract pre-Node-25.
*
* Node-environment tests fall through the `if (typeof window ...)`
* guard and only get the global storage; storage modules' feature-detect
* checks (`typeof localStorage !== 'undefined'`) still work there because
* the global is present and has `setItem`.
*/
class MemoryStorage {
constructor() {
this._data = new Map();
}
get length() {
return this._data.size;
}
key(i) {
return Array.from(this._data.keys())[i] ?? null;
}
getItem(k) {
return this._data.has(String(k)) ? this._data.get(String(k)) : null;
}
setItem(k, v) {
this._data.set(String(k), String(v));
}
removeItem(k) {
this._data.delete(String(k));
}
clear() {
this._data.clear();
}
}
const storage = new MemoryStorage();
const sessionStorage = new MemoryStorage();
Object.defineProperty(globalThis, "localStorage", {
value: storage,
writable: true,
configurable: true,
});
Object.defineProperty(globalThis, "sessionStorage", {
value: sessionStorage,
writable: true,
configurable: true,
});
if (typeof window !== "undefined") {
Object.defineProperty(window, "localStorage", {
value: storage,
writable: true,
configurable: true,
});
Object.defineProperty(window, "sessionStorage", {
value: sessionStorage,
writable: true,
configurable: true,
});
}
// Tell React we're inside an act()-aware environment (jsdom-based
// component tests). Without this, every render triggers a "current
// testing environment is not configured to support act(...)" warning
// to stderr, drowning real diagnostic output.
globalThis.IS_REACT_ACT_ENVIRONMENT = true;