forked from boticlaw/SuperBotijo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
77 lines (69 loc) · 2.78 KB
/
Copy pathvitest.setup.ts
File metadata and controls
77 lines (69 loc) · 2.78 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
// --- React 19 act() fix (DEBE ir primero, antes que @testing-library) ---
//
// React 19 + Vitest 4 + Vite: @vitejs/plugin-react añade "react-server" a
// resolve conditions. react-dom/test-utils (CJS) require("react") internamente
// y resuelve a react-server (sin act). Esto rompe @testing-library/react.
//
// El fix real está en vitest.config.ts (plugin force-react-browser-build +
// conditions browser + deps inline). Si sigue fallando, cada test file
// debe añadir un vi.mock al inicio. Ver comment en vitest.config.ts.
import "@testing-library/jest-dom";
import { vi, afterEach } from "vitest";
// Node 22+ instala un `localStorage` nativo (experimental) en el global que,
// sin --localstorage-file, devuelve `undefined` y ademas impide que jsdom
// instale el suyo propio. Si detectamos que no hay uno funcional, plantamos
// un polyfill spec-compliant para que los hooks/componentes que lo usan
// funcionen igual que en el navegador.
function ensureLocalStorage() {
const existing = (globalThis as { localStorage?: Storage }).localStorage;
try {
if (existing && typeof existing.setItem === "function") {
existing.setItem("__ls_probe__", "1");
existing.removeItem("__ls_probe__");
return; // ya hay uno funcional
}
} catch {
// sigue abajo
}
class LocalStoragePolyfill implements Storage {
private 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)! : null; }
key(index: number): string | null {
const keys = Array.from(this.store.keys());
return index >= 0 && index < keys.length ? keys[index]! : null;
}
removeItem(key: string): void { this.store.delete(key); }
setItem(key: string, value: string): void { this.store.set(key, String(value)); }
}
const polyfill = new LocalStoragePolyfill();
Object.defineProperty(globalThis, "localStorage", {
value: polyfill,
configurable: true,
writable: true,
});
if (typeof window !== "undefined") {
Object.defineProperty(window, "localStorage", {
value: polyfill,
configurable: true,
writable: true,
});
}
}
ensureLocalStorage();
// Set up environment variables for tests before any modules load
vi.stubEnv("JWT_SECRET", "test-secret-key-min-32-chars-long-12345");
vi.stubEnv("AUTH_SECRET", "test-auth-secret-key-min-32-chars-long");
vi.mock("server-only", () => ({}));
// Global cleanup after each test: reset activities-db singleton
afterEach(async () => {
try {
const activitiesDb = await import("@/lib/activities-db");
if (activitiesDb.resetDbForTesting) {
activitiesDb.resetDbForTesting();
}
} catch {
// Ignore if activities-db hasn't been imported
}
});