-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
59 lines (52 loc) · 1.72 KB
/
vitest.setup.ts
File metadata and controls
59 lines (52 loc) · 1.72 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 { vi, beforeEach } from "vitest";
vi.mock("$app/environment", () => ({ browser: true, dev: true, building: false, version: "test" }));
vi.mock("$app/navigation", () => ({
goto: vi.fn(),
invalidate: vi.fn(),
invalidateAll: vi.fn(),
preloadData: vi.fn(),
preloadCode: vi.fn(),
afterNavigate: vi.fn(),
beforeNavigate: vi.fn(),
pushState: vi.fn(),
replaceState: vi.fn(),
}));
vi.mock("$lib/state/settings.svelte", () => ({
settings: {
current: { currency_code: "EUR", date_format: "DD/MM/YYYY", time_format: "24h" },
formatSettings: { currency_code: "EUR", date_format: "DD/MM/YYYY", time_format: "24h" },
load: vi.fn(),
save: vi.fn(),
},
}));
// localStorage: use a simple Map — no class ceremony needed
const localStore = new Map<string, string>();
vi.stubGlobal("localStorage", {
getItem: (k: string) => localStore.get(k) ?? null,
setItem: (k: string, v: string) => localStore.set(k, String(v)),
removeItem: (k: string) => localStore.delete(k),
clear: () => localStore.clear(),
key: (i: number) => Array.from(localStore.keys())[i] ?? null,
get length() { return localStore.size; },
});
// document.documentElement stubs needed by theme + i18n
vi.stubGlobal("document", {
documentElement: {
setAttribute: vi.fn(),
getAttribute: vi.fn().mockReturnValue(null),
classList: { add: vi.fn(), remove: vi.fn(), toggle: vi.fn(), contains: vi.fn().mockReturnValue(false) },
},
});
// matchMedia stub needed by theme (system preference detection)
vi.stubGlobal("matchMedia", vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})));
beforeEach(() => {
localStore.clear();
vi.clearAllMocks();
});