-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvitest.ts
More file actions
259 lines (235 loc) · 9.17 KB
/
vitest.ts
File metadata and controls
259 lines (235 loc) · 9.17 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import assert from "node:assert";
import { relative } from "node:path";
import { pathToFileURL } from "node:url";
import worker from "node:worker_threads";
import { simple as walk } from "acorn-walk";
import { type ESTree } from "meriyah";
import config from "../config";
import fwdSlashPath from "../util/fwdSlashPath";
import {
args as args_,
assignment,
awaitImport,
call_,
identifier,
literal,
member,
memberId,
ret,
} from "../generate";
import { info, warn } from "../message";
import {
abandonProcessRecordingIfNotAlwaysActive,
getTestRecording,
startTestRecording,
} from "../recorder";
import genericTransform from "../transform";
function createInitChannel() {
return new worker.BroadcastChannel("appmap-node/vitest/initialized");
}
// There can be 3 processes (p1, p2, p3) and many threads involved depending on the
// node version and vitest threading arguments. Each thread will load this file so we have to
// determine in which process and thread we are going to listen for initialization message.
// Omitting this ugly check and not preventing "innocent" creation of the initChannel in wrong
// processes and threads results in issues (process hangs). Also, we want to avoid listening in
// more than one thread to prevent multiple info messages.
// p1: node .../.yarn/releases/yarn-3.6.3.cjs vitest run [--no-threads] [--single-thread]
// p2: node .../vitest/vitest.mjs run [--no-threads] [--single-thread]
// p3: node .../vitest/dist/child.js (this process exists only in --no-threads mode)
function shouldListenForInit() {
const isNoThreadsModeMainThread =
worker.isMainThread && process.argv.length > 1 && process.argv[1].endsWith("child.js");
const [major, ,] = process.versions.node.split(".").map(Number);
// There will be 2 threads (#0, #1) in [--no-threads, node 20], so we don't want
// to listen in thread #1 for other modes in this case. On the other hand,
// we don't have thread #2 in [--single-thread node 18], but we can listen
// in thread #1 because we don't have #1 in [--no-threads node 18].
// Thus we guarantee that a single thread listens in all node versions and thread modes combinations.
const threadIdToListenInOtherModes = major >= 20 ? 2 : 1;
return isNoThreadsModeMainThread || worker.threadId === threadIdToListenInOtherModes;
}
if (shouldListenForInit()) {
const initChannel = createInitChannel();
let initialized = false;
initChannel.onmessage = () => {
if (initialized) return;
info("Detected Vitest. Tests will be automatically recorded.");
initialized = true;
};
}
const vitestRunnerIndexJsFilePathEnding = "/@vitest/runner/dist/index.js";
// vitest v3 splits runTest into chunk-hooks.js; index.js is just re-exports
const vitestRunnerChunkHooksJsFilePathEnding = "/@vitest/runner/dist/chunk-hooks.js";
const viteNodeClientMjsFilePathEnding = "/vite-node/dist/client.mjs";
// vitest v4 uses vite's built-in module runner instead of vite-node
const viteModuleRunnerJsFilePathEnding = "/vite/dist/node/module-runner.js";
// vitest v4 uses VitestModuleEvaluator instead of ESModulesEvaluator
const vitestModuleEvaluatorJsFilePathEnding = "/vitest/dist/module-evaluator.js";
export function shouldInstrument(url: URL): boolean {
// 1. …/vite-node/dist/client.mjs ViteNodeRunner.runModule (vitest v0-v3)
// or …/vite/dist/node/module-runner.js ESModulesEvaluator.runInlinedModule (vitest v4)
// is the place to transform test and user files
// 2. @vitest/runner/dist/index.js (or chunk-hooks.js for v3) runTest
// is the place to intercept test before and afters
return (
url.pathname.endsWith(vitestRunnerIndexJsFilePathEnding) ||
url.pathname.endsWith(vitestRunnerChunkHooksJsFilePathEnding) ||
url.pathname.endsWith(viteNodeClientMjsFilePathEnding) ||
url.pathname.endsWith(viteModuleRunnerJsFilePathEnding) ||
url.pathname.endsWith(vitestModuleEvaluatorJsFilePathEnding)
);
}
// Wraps @vitest/runner/dist/index.js runTest
export async function wrapRunTest(
fun: (test: Test, runner: VitestRunner) => unknown,
args: [Test, VitestRunner],
): Promise<unknown> {
const [test] = args;
abandonProcessRecordingIfNotAlwaysActive();
startTestRecording("vitest", ...testNames(test));
// Use try/finally so recording.finish() is always called even when tests throw.
// vitest v1+ propagates test errors through the Promise (unlike v0 which catches
// them internally and resolves with test.result.state = "fail").
let threw = false;
try {
return await fun(...args);
} catch (e) {
threw = true;
throw e;
} finally {
const recording = getTestRecording();
if (test.file?.filepath)
recording.metadata.source_location = fwdSlashPath(
relative(config().root, test.file.filepath),
);
const state = test.result?.state ?? (threw ? "fail" : "pass");
switch (state) {
case "pass":
recording.metadata.test_status = "succeeded";
break;
case "fail":
recording.metadata.test_status = "failed";
recording.metadata.test_failure = {
message: "failed",
location: recording.metadata.source_location,
};
if (test.result?.errors?.length) {
const [{ name, message }] = test.result.errors;
recording.metadata.exception = { class: name, message };
recording.metadata.test_failure.message = message;
}
break;
default:
warn(`Test result not understood for test ${test.name}: ${test.result?.state}`);
}
recording.finish();
}
}
function patchRunTest(fd: ESTree.FunctionDeclaration) {
const wrapped: ESTree.BlockStatement = {
type: "BlockStatement",
body: [
// Statement: return await import(".../vitest.js").wrapRunTest(function runTest(...) {...}, arguments);
ret(
call_(
member(awaitImport(pathToFileURL(__filename).href), identifier(wrapRunTest.name)),
{ ...fd, type: "FunctionExpression" },
args_,
),
),
],
};
fd.body = wrapped;
createInitChannel().postMessage(undefined);
}
export function transformCode(code: string, pathOrUrl: string): string {
const url = pathOrUrl.startsWith("file://") ? new URL(pathOrUrl) : pathToFileURL(pathOrUrl);
return genericTransform(code, url);
}
// Patches …/vite-node/dist/client.mjs ViteNodeRunner.runModule
function patchRunModule(md: ESTree.MethodDefinition) {
// Statement: transformed = await import(".../vitest.js").transformCode(transformed, context.__filename)
const transformCodeStatement = assignment(
identifier("transformed"),
call_(
member(awaitImport(pathToFileURL(__filename).href), identifier(transformCode.name)),
identifier("transformed"),
memberId("context", "__filename"),
),
);
assert(md.value.body);
md.value.body.body.unshift(transformCodeStatement);
}
// Patches …/vite/dist/node/module-runner.js ESModulesEvaluator.runInlinedModule
function patchRunInlinedModule(md: ESTree.MethodDefinition) {
// Statement: code = await import(".../vitest.js").transformCode(code, context["__vite_ssr_import_meta__"].url)
const transformCodeStatement = assignment(
identifier("code"),
call_(
member(awaitImport(pathToFileURL(__filename).href), identifier(transformCode.name)),
identifier("code"),
member(identifier("context"), literal("__vite_ssr_import_meta__"), identifier("url")),
),
);
assert(md.value.body);
md.value.body.body.unshift(transformCodeStatement);
}
export function transform(program: ESTree.Program): ESTree.Program {
const source = program.loc?.source;
if (
source?.endsWith(vitestRunnerIndexJsFilePathEnding) ||
source?.endsWith(vitestRunnerChunkHooksJsFilePathEnding)
)
walk(program, {
FunctionDeclaration(fd: ESTree.FunctionDeclaration) {
if (fd.id?.name === "runTest") patchRunTest(fd);
},
});
else if (source?.endsWith(viteNodeClientMjsFilePathEnding))
walk(program, {
ClassDeclaration(cd: ESTree.ClassDeclaration) {
if (cd.id?.name === "ViteNodeRunner") {
walk(cd, {
MethodDefinition(md: ESTree.MethodDefinition) {
if (md.key?.type === "Identifier" && md.key.name === "runModule") patchRunModule(md);
},
});
}
},
});
else if (
source?.endsWith(viteModuleRunnerJsFilePathEnding) ||
source?.endsWith(vitestModuleEvaluatorJsFilePathEnding)
)
walk(program, {
MethodDefinition(md: ESTree.MethodDefinition) {
if (md.key?.type === "Identifier" && md.key.name === "runInlinedModule")
patchRunInlinedModule(md);
},
});
return program;
}
// https://github.com/vitest-dev/vitest/blob/main/packages/runner/src/types/runner.ts
type VitestRunner = unknown;
// https://github.com/vitest-dev/vitest/blob/main/packages/runner/src/types/tasks.ts
type Test = Task;
interface Task {
name: string;
type?: string;
suite?: Suite;
result?: TaskResult;
file?: { filepath?: string };
}
interface Suite {
name: string;
suite?: Suite;
}
interface TaskResult {
state?: "pass" | "fail";
errors?: Error[];
}
function testNames(test: Test): string[] {
const names = [test.name];
for (let block = test.suite; block?.suite; block = block.suite) names.push(block.name);
return names.reverse();
}