-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
60 lines (56 loc) · 1.95 KB
/
Copy pathvitest.setup.ts
File metadata and controls
60 lines (56 loc) · 1.95 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
import "@testing-library/jest-dom/vitest";
import { afterEach, vi } from "vitest";
import { cleanup } from "@testing-library/react";
// Node.js v26 removed the implicit localStorage in jsdom; provide a mock so
// tests that rely on window.localStorage (theme persistence, wallet session,
// etc.) continue to work.
if (typeof globalThis.localStorage === "undefined") {
const store = new Map<string, string>();
Object.defineProperty(globalThis, "localStorage", {
value: {
getItem: vi.fn((key: string) => store.get(key) ?? null),
setItem: vi.fn((key: string, value: string) => store.set(key, value)),
removeItem: vi.fn((key: string) => store.delete(key)),
clear: vi.fn(() => store.clear()),
get length() {
return store.size;
},
key: vi.fn((index: number) => [...store.keys()][index] ?? null),
},
writable: true,
configurable: true,
});
}
// Unmount rendered components between tests so component tests don't leak
// into one another (auto-cleanup relies on Jest/vitest globals, which this
// project intentionally doesn't enable).
afterEach(() => {
cleanup();
});
// Ensure a simple in‑memory localStorage implementation exists in the jsdom
// environment where `window.localStorage` may be undefined. This mimics the
// browser API sufficiently for the wallet utilities and related tests.
if (typeof window !== "undefined" && !window.localStorage) {
const store = new Map<string, string>();
// @ts-ignore – extending the global window object for test purposes
window.localStorage = {
getItem(key: string) {
return store.has(key) ? store.get(key)! : null;
},
setItem(key: string, value: string) {
store.set(key, value);
},
removeItem(key: string) {
store.delete(key);
},
clear() {
store.clear();
},
key(index: number) {
return Array.from(store.keys())[index] ?? null;
},
get length() {
return store.size;
},
} as Storage;
}