-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
71 lines (64 loc) · 2.02 KB
/
Copy pathvitest.setup.ts
File metadata and controls
71 lines (64 loc) · 2.02 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
// Import @testing-library/jest-dom/vitest for custom matchers
import "@testing-library/jest-dom/vitest";
// Provide a dummy DATABASE_URL so prisma.ts module loads without throwing.
// Tests that need real DB access mock/override as needed.
process.env.DATABASE_URL ??= "postgresql://test:test@localhost:5432/dispatch_test";
// Patch React.act for React 19 + @testing-library/react v16 compat.
// React 19 removed React.act, but older react-dom/test-utils still calls it.
const React = require("react");
if (typeof React.act !== "function") {
Object.defineProperty(React, "act", {
value: function act(cb) {
return typeof cb === "function" ? cb() : cb;
},
writable: true,
configurable: true,
enumerable: false,
});
}
const localStorageStore = new Map<string, string>();
const localStorageMock = {
getItem: (key: string) => localStorageStore.get(key) ?? null,
setItem: (key: string, value: string) => {
localStorageStore.set(key, value);
},
removeItem: (key: string) => {
localStorageStore.delete(key);
},
clear: () => {
localStorageStore.clear();
},
key: (index: number) => Array.from(localStorageStore.keys())[index] ?? null,
get length() {
return localStorageStore.size;
},
};
// Environment-aware: only patch window in browser environments.
if (typeof window !== "undefined") {
Object.defineProperty(window, "localStorage", {
writable: true,
value: localStorageMock,
});
Object.defineProperty(window, "matchMedia", {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
}),
});
}
Object.defineProperty(globalThis, "localStorage", {
writable: true,
value: localStorageMock,
});
// Reset module-level caches between tests for deterministic isolation
import { resetCaches } from "./src/lib/dispatch-env";
beforeEach(() => {
resetCaches();
});