-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
153 lines (133 loc) · 3.63 KB
/
Copy pathvitest.setup.ts
File metadata and controls
153 lines (133 loc) · 3.63 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import "dotenv/config";
import nodeFetch, {
Headers as NodeHeaders,
Request as NodeRequest,
Response as NodeResponse,
} from "node-fetch";
import "@testing-library/jest-dom";
import * as React from "react";
import { afterAll, expect, vi } from "vitest";
vi.mock("server-only", () => ({}));
vi.mock("@plausible-analytics/tracker", () => ({
__esModule: true,
init: vi.fn(),
trackEvent: vi.fn(),
trackPageview: vi.fn(),
enableAutoPageviews: vi.fn(),
}));
vi.mock("posthog-js", () => ({
__esModule: true,
init: vi.fn(),
capture: vi.fn(),
identify: vi.fn(),
reset: vi.fn(),
}));
vi.mock("@posthog/js-lite", () => ({
__esModule: true,
init: vi.fn(),
capture: vi.fn(),
identify: vi.fn(),
reset: vi.fn(),
}));
const ensureJestFacade = () => {
if (typeof globalThis.jest !== "undefined") {
return;
}
const moduleMockRegistry = new Map<string, unknown>();
const originalMock = vi.mock.bind(vi);
const jestMock: typeof vi.mock = (...args) => {
const [moduleId, factory, options] = args;
if (typeof factory === "function") {
return originalMock(
moduleId,
() => {
const mockedModule = factory();
moduleMockRegistry.set(moduleId, mockedModule);
return mockedModule;
},
options,
);
}
moduleMockRegistry.delete(moduleId);
return originalMock(...args);
};
const requireMock = <T = unknown>(moduleId: string): T => {
if (!moduleMockRegistry.has(moduleId)) {
throw new Error(
"Module has not been registered via jest.mock(). Provide a factory when mocking under Vitest so the adapter can expose it synchronously.",
);
}
return moduleMockRegistry.get(moduleId) as T;
};
const clearRegistry = () => moduleMockRegistry.clear();
const jestFacade = {
mock: jestMock,
doMock: vi.doMock.bind(vi),
unmock: vi.unmock.bind(vi),
mocked: vi.mocked.bind(vi),
isMockFunction: vi.isMockFunction.bind(vi),
requireActual: () => {
throw new Error(
"jest.requireActual is not supported by the Vitest compatibility shim. Import the module directly instead.",
);
},
requireMock,
resetModules: () => {
clearRegistry();
vi.resetModules();
},
setTimeout: (timeout: number) => {
if (
typeof (vi as unknown as { setTimeout?: (ms: number) => void })
.setTimeout === "function"
) {
(vi as unknown as { setTimeout: (ms: number) => void }).setTimeout(
timeout,
);
} else {
return undefined;
}
},
clearAllMocks: () => {
clearRegistry();
vi.clearAllMocks();
},
resetAllMocks: () => {
clearRegistry();
vi.resetAllMocks();
},
restoreAllMocks: () => {
clearRegistry();
vi.restoreAllMocks();
},
useFakeTimers: vi.useFakeTimers.bind(vi),
useRealTimers: vi.useRealTimers.bind(vi),
runOnlyPendingTimers: vi.runOnlyPendingTimers.bind(vi),
advanceTimersByTime: vi.advanceTimersByTime.bind(vi),
clearAllTimers: vi.clearAllTimers.bind(vi),
spyOn: vi.spyOn.bind(vi),
fn: vi.fn.bind(vi),
expect,
};
(globalThis as Record<string, unknown>).jest = jestFacade;
};
ensureJestFacade();
(globalThis as Record<string, unknown>).React = React;
if (typeof globalThis.fetch === "undefined") {
globalThis.fetch = nodeFetch as unknown as typeof globalThis.fetch;
}
if (typeof globalThis.Request === "undefined") {
globalThis.Request = NodeRequest as unknown as typeof globalThis.Request;
globalThis.Response = NodeResponse as unknown as typeof globalThis.Response;
globalThis.Headers = NodeHeaders as unknown as typeof globalThis.Headers;
}
afterAll(async () => {
const agent = (
globalThis.fetch as unknown as {
__agent?: { destroy?: () => void };
}
)?.__agent;
if (agent && typeof agent.destroy === "function") {
agent.destroy();
}
});