-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
88 lines (69 loc) · 1.88 KB
/
vitest.setup.ts
File metadata and controls
88 lines (69 loc) · 1.88 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
import '@testing-library/jest-dom/vitest';
import { vi } from 'vitest';
function getVitestWorkerSuffix(): string {
return process.env.VITEST_POOL_ID ?? process.env.VITEST_WORKER_ID ?? '0';
}
function ensureVitestPaths(): void {
const suffix = getVitestWorkerSuffix();
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'test';
}
if (!process.env.CREWMATE_DB_PATH) {
process.env.CREWMATE_DB_PATH = `data/crewmate.test.${suffix}.db`;
}
if (!process.env.CREWMATE_ARTIFACTS_PATH) {
process.env.CREWMATE_ARTIFACTS_PATH = `data/test-artifacts/${suffix}`;
}
}
ensureVitestPaths();
class LocalStorageMock 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 : 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(key, String(value));
}
}
class ResizeObserverMock {
observe(): void {}
unobserve(): void {}
disconnect(): void {}
}
function ensureLocalStorage(): void {
if (typeof window === 'undefined') {
return;
}
const candidate = window.localStorage;
if (
candidate
&& typeof candidate.getItem === 'function'
&& typeof candidate.setItem === 'function'
&& typeof candidate.removeItem === 'function'
) {
return;
}
const storage = new LocalStorageMock();
Object.defineProperty(window, 'localStorage', {
configurable: true,
value: storage,
});
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: storage,
});
}
ensureLocalStorage();
vi.stubGlobal('ResizeObserver', ResizeObserverMock);