forked from openclaw/clawhub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
94 lines (85 loc) · 2.61 KB
/
vitest.setup.ts
File metadata and controls
94 lines (85 loc) · 2.61 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
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const storageData = new WeakMap<Storage, Map<string, string>>();
function getStorageData(storage: Storage) {
let data = storageData.get(storage);
if (!data) {
data = new Map();
storageData.set(storage, data);
}
return data;
}
function installLocalStorageShim() {
if (typeof window === "undefined" || typeof Storage === "undefined") return;
if (typeof window.localStorage?.clear === "function") return;
Object.defineProperties(Storage.prototype, {
length: {
configurable: true,
get() {
return getStorageData(this as Storage).size;
},
},
clear: {
configurable: true,
value() {
getStorageData(this as Storage).clear();
},
},
getItem: {
configurable: true,
value(key: string) {
return getStorageData(this as Storage).get(String(key)) ?? null;
},
},
key: {
configurable: true,
value(index: number) {
return Array.from(getStorageData(this as Storage).keys())[index] ?? null;
},
},
removeItem: {
configurable: true,
value(key: string) {
getStorageData(this as Storage).delete(String(key));
},
},
setItem: {
configurable: true,
value(key: string, value: string) {
getStorageData(this as Storage).set(String(key), String(value));
},
},
});
const localStorage = Object.create(Storage.prototype) as Storage;
storageData.set(localStorage, new Map());
Object.defineProperty(window, "localStorage", {
configurable: true,
value: localStorage,
});
}
installLocalStorageShim();
function proxyReactInternals() {
try {
const rootReact = require("react");
const testingLibraryRequire = createRequire(require.resolve("@testing-library/react"));
const rendererReact = testingLibraryRequire("react");
const rootInternals = rootReact.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
const rendererInternals =
rendererReact.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
if (!rootInternals || !rendererInternals || rootInternals === rendererInternals) return;
for (const key of ["H", "A", "T", "S", "V"] as const) {
Object.defineProperty(rootInternals, key, {
configurable: true,
get() {
return rendererInternals[key];
},
set(value) {
rendererInternals[key] = value;
},
});
}
} catch {
// Best effort for package-manager layouts that install duplicate React copies.
}
}
proxyReactInternals();