-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
92 lines (86 loc) · 2.96 KB
/
vitest.setup.ts
File metadata and controls
92 lines (86 loc) · 2.96 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
import "@testing-library/jest-dom";
import { vi } from "vitest";
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor(callback: ResizeObserverCallback) {}
observe(element: Element): void {}
unobserve(element: Element): void {}
disconnect(): void {}
};
// Ensure `localStorage` is available on the global object (some tests call `localStorage.clear()`)
// Prefer the jsdom `window.localStorage` when present, otherwise provide a simple
// in-memory polyfill that implements the Storage API used by tests.
const ensureLocalStorage = () => {
if (typeof window !== "undefined" && (window as any).localStorage) {
(globalThis as any).localStorage = (window as any).localStorage;
return;
}
const store: Record<string, string> = {};
(globalThis as any).localStorage = {
getItem(key: string) {
return Object.prototype.hasOwnProperty.call(store, key)
? store[key]
: null;
},
setItem(key: string, value: string) {
store[key] = String(value);
},
removeItem(key: string) {
delete store[key];
},
clear() {
for (const k of Object.keys(store)) delete store[k];
},
};
};
ensureLocalStorage();
// If some environment provided a `localStorage` but it's missing `clear()`, override it.
if (typeof (globalThis as any).localStorage?.clear !== "function") {
// Some dependencies (bundled ESM in node_modules) import named exports from the
// CommonJS `cookie` package which can cause ESM resolution errors in the
// Vitest environment. Provide a lightweight mock with `parse` and `serialize`
// to ensure tests that transitively import `cookie` work correctly.
vi.mock("cookie", () => ({
parse: (cookieHeader: string) => {
if (!cookieHeader) return {};
return cookieHeader
.split(";")
.reduce<Record<string, string>>((acc, part) => {
const [rawName, ...rest] = part.split("=");
if (!rawName) return acc;
const name = decodeURIComponent(rawName.trim());
const value = rest.join("=").trim();
acc[name] = decodeURIComponent(value);
return acc;
}, {});
},
serialize: (name: string, val: string) =>
`${encodeURIComponent(name)}=${encodeURIComponent(val)}`,
}));
const existing = (globalThis as any).localStorage || {};
const store: Record<string, string> = {};
try {
// Try to copy existing keys
for (const k of Object.keys(existing)) {
// skip functions
if (typeof (existing as any)[k] !== "function")
store[k] = (existing as any)[k];
}
} catch (e) {}
(globalThis as any).localStorage = {
getItem(key: string) {
return Object.prototype.hasOwnProperty.call(store, key)
? store[key]
: null;
},
setItem(key: string, value: string) {
store[key] = String(value);
},
removeItem(key: string) {
delete store[key];
},
clear() {
for (const k of Object.keys(store)) delete store[k];
},
};
}