Repository navigation
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
63 lines (58 loc) · 2.17 KB
/
Copy pathvitest.setup.ts
File metadata and controls
63 lines (58 loc) · 2.17 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
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { afterEach } from "vitest";
// Node 25 ships a global Web Storage that is misconfigured here (no backing
// file) and shadows jsdom's Storage, leaving window.localStorage as a method-less
// object. Install a real in-memory Storage so localStorage-backed code is testable.
class MemoryStorage implements Storage {
#store = new Map<string, string>();
get length(): number {
return this.#store.size;
}
clear(): void {
this.#store.clear();
}
getItem(key: string): string | null {
return this.#store.has(key) ? (this.#store.get(key) as string) : null;
}
key(index: number): string | null {
return Array.from(this.#store.keys())[index] ?? null;
}
removeItem(key: string): void {
this.#store.delete(key);
}
setItem(key: string, value: string): void {
this.#store.set(String(key), String(value));
}
}
Object.defineProperty(globalThis, "localStorage", { value: new MemoryStorage(), configurable: true, writable: true });
Object.defineProperty(globalThis, "sessionStorage", { value: new MemoryStorage(), configurable: true, writable: true });
// RTL only auto-registers cleanup when Vitest globals are enabled; we keep
// globals off and use explicit imports, so unmount + reset storage between tests.
afterEach(() => {
cleanup();
globalThis.localStorage.clear();
});
// jsdom does not implement matchMedia, which the docs navigation uses.
if (typeof window !== "undefined" && !window.matchMedia) {
window.matchMedia = (query: string) =>
({
matches: false,
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
}) as unknown as MediaQueryList;
}
// jsdom has no layout, so observers that redraw on resize (the annotated product figures) never fire.
const layoutGlobals = globalThis as { ResizeObserver?: typeof ResizeObserver };
if (!layoutGlobals.ResizeObserver) {
layoutGlobals.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
} as unknown as typeof ResizeObserver;
}