vitest 4.1.12 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +192 -10
- package/browser/context.d.ts +7 -1
- package/browser/context.js +5 -5
- package/config.d.ts +0 -2
- package/dist/browser.d.ts +52 -21
- package/dist/browser.js +12 -13
- package/dist/chunks/{acorn.B2iPLyUM.js → acorn.C1kjbUFw.js} +30 -30
- package/dist/chunks/{base.B6Opl8PE.js → base.Cof-UHJf.js} +81 -52
- package/dist/chunks/{browser.d.BcoexmFG.d.ts → browser.d.C7tWgUvw.d.ts} +17 -14
- package/dist/chunks/{cac.DzDtg8hz.js → cac.BYbt0aJp.js} +143 -87
- package/dist/chunks/{cli-api.B0Pz1_R7.js → cli-api.COfZo8lK.js} +7160 -7189
- package/dist/chunks/config.d.B3YV4t0O.d.ts +3873 -0
- package/dist/chunks/{console.3WNpx0tS.js → console.CQZ877Hi.js} +69 -63
- package/dist/chunks/{constants.CPYnjOGj.js → constants.DLYM6cZY.js} +2 -2
- package/dist/chunks/coverage.9iqXE2gN.js +26 -0
- package/dist/chunks/{coverage.CTzCuANN.js → coverage.jGCktcGP.js} +7 -2
- package/dist/chunks/{creator.DgVhQm5q.js → creator.DHWmytIK.js} +65 -28
- package/dist/chunks/{defaults.9aQKnqFk.js → defaults.B1FCaFfh.js} +16 -9
- package/dist/chunks/display.BlTmubjj.js +194 -0
- package/dist/chunks/doctor.B-oPbwps.js +465 -0
- package/dist/chunks/{env.D4Lgay0q.js → env.BKKtU2WC.js} +2 -1
- package/dist/chunks/{environment.d.CrsxCzP1.d.ts → environment.d.BFQtFKdZ.d.ts} +9 -1
- package/dist/chunks/globals.ZKtKSkEf.js +29 -0
- package/dist/chunks/index.Bi0NXYPf.js +1708 -0
- package/dist/chunks/{index.CMESou6r.js → index.BoyTG_B7.js} +82 -81
- package/dist/chunks/index.CZzC1gGg.js +8501 -0
- package/dist/chunks/index.D0heQG4v.js +1318 -0
- package/dist/chunks/index.Di8au7q4.js +15665 -0
- package/dist/chunks/{index.Chj8NDwU.js → index.DmDMHCg8.js} +6 -12
- package/dist/chunks/{index.DXx9Dtk7.js → index.w_oBZ9N8.js} +25 -12
- package/dist/chunks/{init-forks.H5ZuobOQ.js → init-forks.BaDXQSD8.js} +7 -10
- package/dist/chunks/{init-threads.6kl1khcL.js → init-threads.ZX_Vov0z.js} +1 -1
- package/dist/chunks/{init.k9zZ9sLh.js → init.CO4AB7HZ.js} +113 -38
- package/dist/chunks/module-dependencies._2Eei3FO.js +71 -0
- package/dist/chunks/{native.DPzPHdi5.js → native.upB-V0CK.js} +50 -37
- package/dist/chunks/{nativeModuleMocker.BkNfQMkH.js → nativeModuleMocker.Dh5azSB4.js} +48 -19
- package/dist/chunks/{index.BCY_7LL2.js → nativeModuleRunner.9xPIMzi7.js} +110 -42
- package/dist/chunks/node.B9mkVmLH.js +47 -0
- package/dist/chunks/offset.Dy-5Fdfn.js +24 -0
- package/dist/chunks/pathe.M-eThtNZ.zq3TM4NT.js +492 -0
- package/dist/chunks/{reporters.d.DtoKVV2s.d.ts → plugin.d.LRFSN46m.d.ts} +2573 -1453
- package/dist/chunks/plugins.DPQHVT0p.js +37 -0
- package/dist/chunks/rawSnapshot.DqSiqIEx.js +356 -0
- package/dist/chunks/resolver.BzpmeQeT.js +48 -0
- package/dist/chunks/rpc.Uhkwr2NM.js +93 -0
- package/dist/chunks/rpc.d.3FOhdFx8.d.ts +75 -0
- package/dist/chunks/run.CjdgFZiW.js +4231 -0
- package/dist/chunks/setup-common.CqAQCXzx.js +36 -0
- package/dist/chunks/source-map.DakdQc29.js +1788 -0
- package/dist/chunks/source-map.d.BkG8YK_3.d.ts +89 -0
- package/dist/chunks/spy.DDMXghXv.js +509 -0
- package/dist/chunks/task-utils.d.D-gFPNFi.d.ts +144 -0
- package/dist/chunks/tinyrainbow.Do8I1XRG.js +88 -0
- package/dist/chunks/{utils.BS4fH3nR.js → utils.BBk_icFG.js} +50 -52
- package/dist/chunks/{utils.BX5Fg8C4.js → utils.DYj33du9.js} +1 -17
- package/dist/chunks/vm.b4MbCAJQ.js +1329 -0
- package/dist/chunks/{worker.d.ZpHpO4yb.d.ts → worker.d.CyJRpyfZ.d.ts} +34 -19
- package/dist/cli.js +5 -6
- package/dist/config.cjs +9 -6
- package/dist/config.d.ts +27 -25
- package/dist/config.js +3 -3
- package/dist/index.d.ts +471 -38
- package/dist/index.js +18 -19
- package/dist/module-evaluator.d.ts +64 -5
- package/dist/module-evaluator.js +122 -62
- package/dist/node.d.ts +355 -87
- package/dist/node.js +49 -73
- package/dist/runtime.d.ts +43 -6
- package/dist/runtime.js +7 -9
- package/dist/spy.js +1 -1
- package/dist/task-utils.js +290 -0
- package/dist/{chunks/traces.DT5aQ62U.js → traces.js} +3 -2
- package/dist/worker.d.ts +8 -10
- package/dist/worker.js +33 -43
- package/dist/workers/forks.js +30 -40
- package/dist/workers/runVmTests.js +66 -43
- package/dist/workers/threads.js +30 -40
- package/dist/workers/vmForks.js +19 -20
- package/dist/workers/vmThreads.js +19 -20
- package/globals.d.ts +19 -19
- package/index.cjs +3 -3
- package/optional-runtime-types.d.ts +1 -1
- package/optional-types.d.ts +1 -1
- package/package.json +59 -74
- package/suppress-warnings.cjs +1 -1
- package/dist/chunks/_commonjsHelpers.D26ty3Ew.js +0 -6
- package/dist/chunks/benchmark.CX_oY03V.js +0 -40
- package/dist/chunks/benchmark.d.DAaHLpsq.d.ts +0 -24
- package/dist/chunks/config.d.A1h_Y6Jt.d.ts +0 -232
- package/dist/chunks/coverage.DM_a_rWm.js +0 -1087
- package/dist/chunks/coverage.d.BZtK59WP.d.ts +0 -37
- package/dist/chunks/evaluatedModules.Dg1zASAC.js +0 -17
- package/dist/chunks/global.d.DVsSRdQ5.d.ts +0 -100
- package/dist/chunks/globals.Dj1TGiMC.js +0 -31
- package/dist/chunks/index.DC7d2Pf8.js +0 -729
- package/dist/chunks/index.DdgEv5B1.js +0 -42
- package/dist/chunks/index.UpGiHP7g.js +0 -4255
- package/dist/chunks/index.og1WyBLx.js +0 -6358
- package/dist/chunks/nativeModuleRunner.BIakptoF.js +0 -36
- package/dist/chunks/node.COQbm6gK.js +0 -14
- package/dist/chunks/plugin.d.DwFIiJ7i.d.ts +0 -38
- package/dist/chunks/rpc.MzXet3jl.js +0 -144
- package/dist/chunks/rpc.d.B_8sPU0w.d.ts +0 -30
- package/dist/chunks/setup-common.DYx3LtFI.js +0 -60
- package/dist/chunks/startVitestModuleRunner.DB-7oCpn.js +0 -740
- package/dist/chunks/suite.d.udJtyAgw.d.ts +0 -10
- package/dist/chunks/test.DNmyFkvJ.js +0 -4430
- package/dist/chunks/traces.d.D2T_R8rx.d.ts +0 -60
- package/dist/chunks/vm.CXMd5FHa.js +0 -785
- package/dist/coverage.d.ts +0 -123
- package/dist/coverage.js +0 -27
- package/dist/environments.d.ts +0 -22
- package/dist/environments.js +0 -5
- package/dist/reporters.d.ts +0 -27
- package/dist/reporters.js +0 -26
- package/dist/runners.d.ts +0 -70
- package/dist/runners.js +0 -19
- package/dist/snapshot.d.ts +0 -9
- package/dist/snapshot.js +0 -6
- package/dist/suite.d.ts +0 -5
- package/dist/suite.js +0 -8
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { performance } from 'node:perf_hooks';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
import { t as toArray, r as resolve } from './pathe.M-eThtNZ.zq3TM4NT.js';
|
|
8
|
+
import { $ } from './tinyrainbow.Do8I1XRG.js';
|
|
9
|
+
import { distDir } from '../path.js';
|
|
10
|
+
|
|
11
|
+
// custom environments are measured too: the run fails if they lack setupVM
|
|
12
|
+
const ENVIRONMENTS_WITHOUT_VM_UPSIDE = /* @__PURE__ */ new Set(["node", "edge-runtime"]);
|
|
13
|
+
/**
|
|
14
|
+
* Builds the list of configurations worth measuring for this config. The
|
|
15
|
+
* candidates are pruned by what the config already uses - there is no point
|
|
16
|
+
* in measuring `threads` when the config already runs in `threads`.
|
|
17
|
+
*/
|
|
18
|
+
function resolveDoctorCandidates(projects, options = {}) {
|
|
19
|
+
const candidates = [];
|
|
20
|
+
// `pool`, `environment` and the fs module cache don't reach the browser
|
|
21
|
+
// runtime, so those candidates are driven by the node-side projects only;
|
|
22
|
+
// isolation applies to browser projects as well
|
|
23
|
+
const testProjects = projects.filter((project) => !project.browser);
|
|
24
|
+
const pools = new Set(testProjects.map((project) => project.pool));
|
|
25
|
+
const usesVmPool = pools.has("vmThreads") || pools.has("vmForks");
|
|
26
|
+
const benefitsFromVm = testProjects.some((project) => !ENVIRONMENTS_WITHOUT_VM_UPSIDE.has(project.environment));
|
|
27
|
+
const isolates = projects.some((project) => project.isolate && (project.browser || project.pool !== "vmThreads" && project.pool !== "vmForks"));
|
|
28
|
+
if (pools.has("forks")) candidates.push({
|
|
29
|
+
id: "threads",
|
|
30
|
+
title: `pool: 'threads'`,
|
|
31
|
+
overrides: { pool: "threads" },
|
|
32
|
+
configLines: [`pool: 'threads'`],
|
|
33
|
+
preservesIsolation: true
|
|
34
|
+
});
|
|
35
|
+
if (benefitsFromVm && !pools.has("vmThreads")) candidates.push({
|
|
36
|
+
id: "vmThreads",
|
|
37
|
+
title: `pool: 'vmThreads'`,
|
|
38
|
+
overrides: { pool: "vmThreads" },
|
|
39
|
+
configLines: [`pool: 'vmThreads'`],
|
|
40
|
+
preservesIsolation: true
|
|
41
|
+
});
|
|
42
|
+
// vmForks trades vmThreads' worker threads for child processes: each child
|
|
43
|
+
// gets its own heap and GC, which can beat vmThreads on GC-heavy suites, and
|
|
44
|
+
// it is the vm option for suites that cannot run in worker threads
|
|
45
|
+
if (benefitsFromVm && !pools.has("vmForks")) candidates.push({
|
|
46
|
+
id: "vmForks",
|
|
47
|
+
title: `pool: 'vmForks'`,
|
|
48
|
+
overrides: { pool: "vmForks" },
|
|
49
|
+
configLines: [`pool: 'vmForks'`],
|
|
50
|
+
preservesIsolation: true
|
|
51
|
+
});
|
|
52
|
+
// jsdom -> happy-dom is the only swap with a speed upside; it is applied per
|
|
53
|
+
// project, so projects running other environments keep them
|
|
54
|
+
if (options.happyDomAvailable && testProjects.some((project) => project.environment === "jsdom")) candidates.push({
|
|
55
|
+
id: "happy-dom",
|
|
56
|
+
title: `environment: 'happy-dom'`,
|
|
57
|
+
overrides: {},
|
|
58
|
+
envSwap: {
|
|
59
|
+
from: "jsdom",
|
|
60
|
+
to: "happy-dom"
|
|
61
|
+
},
|
|
62
|
+
configLines: [`environment: 'happy-dom'`],
|
|
63
|
+
preservesIsolation: true
|
|
64
|
+
});
|
|
65
|
+
if (isolates) candidates.push({
|
|
66
|
+
id: "no-isolate",
|
|
67
|
+
title: "isolate: false",
|
|
68
|
+
overrides: { isolate: false },
|
|
69
|
+
configLines: ["isolate: false"],
|
|
70
|
+
preservesIsolation: false,
|
|
71
|
+
validateIsolation: true
|
|
72
|
+
});
|
|
73
|
+
if (usesVmPool)
|
|
74
|
+
// the honest competitor of a vm pool: reused workers with shared state
|
|
75
|
+
candidates.push({
|
|
76
|
+
id: "threads-no-isolate",
|
|
77
|
+
title: `pool: 'threads' + isolate: false`,
|
|
78
|
+
overrides: {
|
|
79
|
+
pool: "threads",
|
|
80
|
+
isolate: false
|
|
81
|
+
},
|
|
82
|
+
configLines: [`pool: 'threads'`, "isolate: false"],
|
|
83
|
+
preservesIsolation: false,
|
|
84
|
+
validateIsolation: true
|
|
85
|
+
});
|
|
86
|
+
if (testProjects.length > 0 && testProjects.every((project) => !project.fsModuleCache))
|
|
87
|
+
// an untimed priming run populates the cache first: the candidate measures
|
|
88
|
+
// what repeated runs pay, which is what doctor compares everywhere else
|
|
89
|
+
candidates.push({
|
|
90
|
+
id: "fs-cache",
|
|
91
|
+
title: "fsModuleCache: true",
|
|
92
|
+
overrides: { fsModuleCache: true },
|
|
93
|
+
configLines: ["fsModuleCache: true"],
|
|
94
|
+
preservesIsolation: true,
|
|
95
|
+
primeRuns: 1
|
|
96
|
+
});
|
|
97
|
+
return candidates;
|
|
98
|
+
}
|
|
99
|
+
function log(...args) {
|
|
100
|
+
console.log(...args);
|
|
101
|
+
}
|
|
102
|
+
async function doctor(cliFilters, options) {
|
|
103
|
+
const { prepareVitest } = await import('./cli-api.COfZo8lK.js').then(function (n) { return n.j; });
|
|
104
|
+
log();
|
|
105
|
+
log($.inverse($.bold($.blue(" DOCTOR "))), "resolving the current configuration...");
|
|
106
|
+
const ctx = await prepareVitest({
|
|
107
|
+
...options,
|
|
108
|
+
watch: false,
|
|
109
|
+
run: true
|
|
110
|
+
}, void 0, void 0, cliFilters);
|
|
111
|
+
const projects = ctx.projects.map((project) => ({
|
|
112
|
+
name: project.name,
|
|
113
|
+
pool: project.config.pool,
|
|
114
|
+
environment: project.config.environment,
|
|
115
|
+
isolate: project.config.isolate,
|
|
116
|
+
browser: project.config.browser.enabled,
|
|
117
|
+
fsModuleCache: project.config.fsModuleCache === true
|
|
118
|
+
}));
|
|
119
|
+
const fileCount = (await ctx.getRelevantTestSpecifications(cliFilters)).length;
|
|
120
|
+
const effectiveMaxWorkers = ctx.config.maxWorkers;
|
|
121
|
+
await ctx.close();
|
|
122
|
+
// the environments import 'happy-dom' relative to the vitest package (it is
|
|
123
|
+
// a peer dependency), so resolving from here mirrors what a run would do
|
|
124
|
+
let happyDomAvailable = false;
|
|
125
|
+
try {
|
|
126
|
+
import.meta.resolve("happy-dom");
|
|
127
|
+
happyDomAvailable = true;
|
|
128
|
+
} catch {}
|
|
129
|
+
const candidates = resolveDoctorCandidates(projects, { happyDomAvailable });
|
|
130
|
+
const baselineTitle = `baseline (${describeProjects(projects)})`;
|
|
131
|
+
// every measured run goes through one generated runner script: candidates
|
|
132
|
+
// may need per-project overrides (the happy-dom swap), which the CLI cannot
|
|
133
|
+
// express, and routing the baseline through the same entry keeps the
|
|
134
|
+
// process cost identical across measurements
|
|
135
|
+
const runnerDir = mkdtempSync(join(tmpdir(), "vitest-doctor-"));
|
|
136
|
+
const runnerPath = join(runnerDir, "runner.mjs");
|
|
137
|
+
writeFileSync(runnerPath, createRunnerScript());
|
|
138
|
+
process.once("exit", () => rmSync(runnerDir, {
|
|
139
|
+
recursive: true,
|
|
140
|
+
force: true
|
|
141
|
+
}));
|
|
142
|
+
const childProjects = toArray(options.project).map(String);
|
|
143
|
+
const childOptions = {
|
|
144
|
+
watch: false,
|
|
145
|
+
...options.root ? { root: String(options.root) } : {},
|
|
146
|
+
...options.config ? { config: String(options.config) } : {},
|
|
147
|
+
...childProjects.length ? { project: childProjects } : {}
|
|
148
|
+
};
|
|
149
|
+
// make sure an interrupted doctor doesn't leave a test suite running
|
|
150
|
+
let activeChild;
|
|
151
|
+
const killActiveChild = () => {
|
|
152
|
+
activeChild?.kill("SIGKILL");
|
|
153
|
+
process.exit(130);
|
|
154
|
+
};
|
|
155
|
+
process.once("SIGINT", killActiveChild);
|
|
156
|
+
process.once("SIGTERM", killActiveChild);
|
|
157
|
+
const runVitest = (run, timeoutMs) => {
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
const start = performance.now();
|
|
160
|
+
const payload = JSON.stringify({
|
|
161
|
+
filters: cliFilters,
|
|
162
|
+
options: {
|
|
163
|
+
...childOptions,
|
|
164
|
+
...run.overrides
|
|
165
|
+
},
|
|
166
|
+
envSwap: run.envSwap
|
|
167
|
+
});
|
|
168
|
+
const child = spawn(process.execPath, [runnerPath, payload], {
|
|
169
|
+
env: {
|
|
170
|
+
...process.env,
|
|
171
|
+
NO_COLOR: "1"
|
|
172
|
+
},
|
|
173
|
+
stdio: [
|
|
174
|
+
"ignore",
|
|
175
|
+
"ignore",
|
|
176
|
+
"pipe"
|
|
177
|
+
]
|
|
178
|
+
});
|
|
179
|
+
activeChild = child;
|
|
180
|
+
let timedOut = false;
|
|
181
|
+
const timer = timeoutMs ? setTimeout(() => {
|
|
182
|
+
timedOut = true;
|
|
183
|
+
child.kill("SIGKILL");
|
|
184
|
+
}, timeoutMs) : void 0;
|
|
185
|
+
let stderr = "";
|
|
186
|
+
child.stderr.on("data", (chunk) => {
|
|
187
|
+
stderr = (stderr + String(chunk)).slice(-4e3);
|
|
188
|
+
});
|
|
189
|
+
child.on("close", (code) => {
|
|
190
|
+
if (timer) clearTimeout(timer);
|
|
191
|
+
activeChild = void 0;
|
|
192
|
+
if (timedOut) stderr += `\n(the run was killed after exceeding ${Math.round((timeoutMs || 0) / 1e3)}s - several times the baseline duration)`;
|
|
193
|
+
resolve({
|
|
194
|
+
wall: performance.now() - start,
|
|
195
|
+
ok: code === 0 && !timedOut,
|
|
196
|
+
stderr
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
log();
|
|
202
|
+
if (candidates.length === 0) log("The configuration already uses the fastest setup Vitest knows how to compare - measuring it for reference.");
|
|
203
|
+
else {
|
|
204
|
+
log("Measuring alternative configurations by running the test suite under each of them.");
|
|
205
|
+
log($.dim("Close other heavy programs - the comparison is only as good as the machine is quiet."));
|
|
206
|
+
}
|
|
207
|
+
log();
|
|
208
|
+
// The first baseline run also warms up caches for every following run, so
|
|
209
|
+
// candidates are compared in a warm steady state.
|
|
210
|
+
process.stdout.write($.dim(` measuring ${baselineTitle}...`));
|
|
211
|
+
const firstRun = await runVitest({});
|
|
212
|
+
if (!firstRun.ok) {
|
|
213
|
+
process.stdout.write("\n");
|
|
214
|
+
log($.red("The test suite fails with the current configuration. Fix the failures first - doctor needs a green suite to compare configurations."));
|
|
215
|
+
if (firstRun.stderr.trim()) log($.dim(firstRun.stderr.trim()));
|
|
216
|
+
process.exitCode = 1;
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
// scale repetitions to the suite duration: short suites are noisy and can
|
|
220
|
+
// afford more runs, long suites cannot
|
|
221
|
+
const reps = firstRun.wall < 1e4 ? 3 : firstRun.wall < 6e4 ? 2 : 1;
|
|
222
|
+
// the first run is cold and mostly warms up caches: always take at least one
|
|
223
|
+
// more baseline run so the reported baseline is a warm one, like the
|
|
224
|
+
// candidates that run after it
|
|
225
|
+
const baselineRuns = Math.max(reps, 2);
|
|
226
|
+
const baselineWalls = [firstRun.wall];
|
|
227
|
+
for (let i = 1; i < baselineRuns; i++) {
|
|
228
|
+
const run = await runVitest({});
|
|
229
|
+
if (!run.ok) {
|
|
230
|
+
process.stdout.write("\n");
|
|
231
|
+
log($.red(`The test suite passed once but failed on repetition ${i + 1} with the same configuration - doctor needs a stable green suite to compare configurations.`));
|
|
232
|
+
if (run.stderr.trim()) log($.dim(run.stderr.trim()));
|
|
233
|
+
process.exitCode = 1;
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
baselineWalls.push(run.wall);
|
|
237
|
+
}
|
|
238
|
+
const baselineWall = Math.min(...baselineWalls);
|
|
239
|
+
process.stdout.write(` ${formatSeconds(baselineWall)}\n`);
|
|
240
|
+
// a candidate that takes several times the baseline is a regression, not a
|
|
241
|
+
// recommendation - don't let it stall doctor indefinitely
|
|
242
|
+
const candidateTimeout = Math.max(6e4, baselineWall * 4);
|
|
243
|
+
const measured = [];
|
|
244
|
+
for (const candidate of candidates) {
|
|
245
|
+
process.stdout.write($.dim(` measuring ${candidate.title}...`));
|
|
246
|
+
let wall = Number.POSITIVE_INFINITY;
|
|
247
|
+
let ok = true;
|
|
248
|
+
let stderr = "";
|
|
249
|
+
const candidateRun = {
|
|
250
|
+
overrides: candidate.overrides,
|
|
251
|
+
envSwap: candidate.envSwap
|
|
252
|
+
};
|
|
253
|
+
for (let i = 0; i < (candidate.primeRuns ?? 0) && ok; i++) {
|
|
254
|
+
const run = await runVitest(candidateRun, candidateTimeout);
|
|
255
|
+
ok = run.ok;
|
|
256
|
+
stderr = run.stderr;
|
|
257
|
+
}
|
|
258
|
+
for (let i = 0; i < reps && ok; i++) {
|
|
259
|
+
const run = await runVitest(candidateRun, candidateTimeout);
|
|
260
|
+
wall = Math.min(wall, run.wall);
|
|
261
|
+
ok = run.ok;
|
|
262
|
+
stderr = run.stderr;
|
|
263
|
+
}
|
|
264
|
+
let isolationVerdict;
|
|
265
|
+
if (ok && candidate.validateIsolation) {
|
|
266
|
+
// tests can pass under shared state by accident of ordering: shuffle the
|
|
267
|
+
// file order twice to catch the common cross-file couplings; the explicit
|
|
268
|
+
// distinct seeds guarantee two different orders even when the user pinned
|
|
269
|
+
// `sequence.seed` in their config
|
|
270
|
+
isolationVerdict = "passed";
|
|
271
|
+
for (let i = 0; i < 2; i++) {
|
|
272
|
+
const run = await runVitest({
|
|
273
|
+
overrides: {
|
|
274
|
+
...candidate.overrides,
|
|
275
|
+
sequence: {
|
|
276
|
+
shuffle: { files: true },
|
|
277
|
+
seed: 271828 + i
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
envSwap: candidate.envSwap
|
|
281
|
+
}, candidateTimeout);
|
|
282
|
+
if (!run.ok) {
|
|
283
|
+
isolationVerdict = "failed";
|
|
284
|
+
stderr = run.stderr;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
measured.push({
|
|
290
|
+
candidate,
|
|
291
|
+
wall,
|
|
292
|
+
ok,
|
|
293
|
+
stderr,
|
|
294
|
+
isolationVerdict
|
|
295
|
+
});
|
|
296
|
+
process.stdout.write(ok ? ` ${formatSeconds(wall)}\n` : ` ${$.red("failed")}\n`);
|
|
297
|
+
}
|
|
298
|
+
const viable = measured.filter((result) => result.ok && result.isolationVerdict !== "failed" && result.wall < baselineWall * .9);
|
|
299
|
+
// among candidates close to the fastest, prefer the one that keeps per-file
|
|
300
|
+
// isolation - equal speed with stronger guarantees wins
|
|
301
|
+
const fastest = viable.length > 0 ? viable.reduce((a, b) => b.wall < a.wall ? b : a) : void 0;
|
|
302
|
+
const preferred = fastest ? viable.filter((result) => result.wall <= fastest.wall * 1.05).sort((a, b) => Number(b.candidate.preservesIsolation) - Number(a.candidate.preservesIsolation))[0] : void 0;
|
|
303
|
+
// Past a certain worker count the single main-thread Vite server becomes the
|
|
304
|
+
// bottleneck, so FEWER workers can be faster. Greedily descend by halving on
|
|
305
|
+
// top of the winning configuration, keeping a step only when it is a real
|
|
306
|
+
// (>= 5%) improvement.
|
|
307
|
+
const workerStack = preferred ? {
|
|
308
|
+
overrides: preferred.candidate.overrides,
|
|
309
|
+
envSwap: preferred.candidate.envSwap
|
|
310
|
+
} : {};
|
|
311
|
+
const workerSuffix = preferred ? ` (with ${preferred.candidate.title})` : "";
|
|
312
|
+
const workerProbes = [];
|
|
313
|
+
let bestWorkers;
|
|
314
|
+
{
|
|
315
|
+
let currentBest = preferred ? preferred.wall : baselineWall;
|
|
316
|
+
let probe = Math.floor(effectiveMaxWorkers / 2);
|
|
317
|
+
while (probe >= 2 && probe < fileCount) {
|
|
318
|
+
process.stdout.write($.dim(` measuring maxWorkers: ${probe}${workerSuffix}...`));
|
|
319
|
+
let wall = Number.POSITIVE_INFINITY;
|
|
320
|
+
let ok = true;
|
|
321
|
+
for (let i = 0; i < reps && ok; i++) {
|
|
322
|
+
const run = await runVitest({
|
|
323
|
+
overrides: {
|
|
324
|
+
...workerStack.overrides,
|
|
325
|
+
maxWorkers: probe
|
|
326
|
+
},
|
|
327
|
+
envSwap: workerStack.envSwap
|
|
328
|
+
}, candidateTimeout);
|
|
329
|
+
wall = Math.min(wall, run.wall);
|
|
330
|
+
ok = run.ok;
|
|
331
|
+
}
|
|
332
|
+
process.stdout.write(ok ? ` ${formatSeconds(wall)}\n` : ` ${$.red("failed")}\n`);
|
|
333
|
+
if (!ok) break;
|
|
334
|
+
workerProbes.push({
|
|
335
|
+
workers: probe,
|
|
336
|
+
wall
|
|
337
|
+
});
|
|
338
|
+
if (wall >= currentBest * .95) break;
|
|
339
|
+
currentBest = wall;
|
|
340
|
+
bestWorkers = {
|
|
341
|
+
workers: probe,
|
|
342
|
+
wall
|
|
343
|
+
};
|
|
344
|
+
probe = Math.floor(probe / 2);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
log();
|
|
348
|
+
log($.bold("Results") + $.dim(` (min of ${reps} run${reps === 1 ? "" : "s"} each)`));
|
|
349
|
+
log();
|
|
350
|
+
const rowTitles = [...measured.map((m) => m.candidate.title), ...workerProbes.map((p) => `maxWorkers: ${p.workers}${workerSuffix}`)];
|
|
351
|
+
const width = Math.max(baselineTitle.length, ...rowTitles.map((title) => title.length)) + 2;
|
|
352
|
+
log(` ${baselineTitle.padEnd(width)}${formatSeconds(baselineWall)}`);
|
|
353
|
+
for (const result of measured) {
|
|
354
|
+
const title = result.candidate.title.padEnd(width);
|
|
355
|
+
if (!result.ok) log(` ${title}${$.red("failed")}`);
|
|
356
|
+
else if (result.isolationVerdict === "failed") log(` ${title}${formatSeconds(result.wall)} ${$.red("(fails with a shuffled file order - tests depend on isolation)")}`);
|
|
357
|
+
else log(` ${title}${formatSeconds(result.wall)} ${formatDelta(result.wall, baselineWall)}`);
|
|
358
|
+
}
|
|
359
|
+
for (const probeResult of workerProbes) log(` ${`maxWorkers: ${probeResult.workers}${workerSuffix}`.padEnd(width)}${formatSeconds(probeResult.wall)} ${formatDelta(probeResult.wall, baselineWall)}`);
|
|
360
|
+
// failing candidates are as informative as fast ones: show what broke,
|
|
361
|
+
// so "vmThreads is not an option for this suite" comes with the reason
|
|
362
|
+
for (const result of measured) if (!result.ok) logFailureExcerpt(`${result.candidate.title} failed with:`, result.stderr);
|
|
363
|
+
else if (result.isolationVerdict === "failed") logFailureExcerpt(`${result.candidate.title} failed with a shuffled file order:`, result.stderr);
|
|
364
|
+
log();
|
|
365
|
+
if (!preferred && !bestWorkers) {
|
|
366
|
+
if (candidates.length === 0) log(`${$.bold("Recommendation:")} keep the current configuration (${describeProjects(projects)}) - it measured ${$.yellow(formatSeconds(baselineWall))} and Vitest has no faster candidate to suggest for it.`);
|
|
367
|
+
else log(`${$.bold("Recommendation:")} keep the current configuration (${describeProjects(projects)}) - no measured candidate was more than 10% faster.`);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const finalWall = bestWorkers ? bestWorkers.wall : preferred.wall;
|
|
371
|
+
const recommendationTitle = [preferred?.candidate.title, bestWorkers ? `maxWorkers: ${bestWorkers.workers}` : void 0].filter(Boolean).join(" + ");
|
|
372
|
+
const configLines = [...preferred ? preferred.candidate.configLines : [], ...bestWorkers ? [`maxWorkers: ${bestWorkers.workers}`] : []];
|
|
373
|
+
log(`${$.bold("Recommendation:")} ${$.yellow(recommendationTitle)} ${formatDelta(finalWall, baselineWall)}`);
|
|
374
|
+
log();
|
|
375
|
+
log($.dim(" // vitest.config.ts"));
|
|
376
|
+
log($.dim(` import { defineConfig } from 'vitest/config'`));
|
|
377
|
+
log();
|
|
378
|
+
log($.dim(" export default defineConfig({"));
|
|
379
|
+
log($.dim(" test: {"));
|
|
380
|
+
for (const [index, line] of configLines.entries()) log(` ${line},${index === configLines.length - 1 ? $.dim(` // measured ${Math.round((finalWall - baselineWall) / baselineWall * 100)}% on this suite`) : ""}`);
|
|
381
|
+
log($.dim(" },"));
|
|
382
|
+
log($.dim(" })"));
|
|
383
|
+
log();
|
|
384
|
+
if (preferred) for (const note of candidateNotes(preferred)) log($.dim(` ${note}`));
|
|
385
|
+
if (bestWorkers) {
|
|
386
|
+
log($.dim(` Fewer workers can be faster because every worker funnels transforms through the`));
|
|
387
|
+
log($.dim(` single main-thread Vite server; a lower count also reduces memory pressure.`));
|
|
388
|
+
}
|
|
389
|
+
if (projects.length > 1) {
|
|
390
|
+
const note = preferred?.candidate.envSwap ? `Doctor swapped the environment of every project running ${preferred.candidate.envSwap.from}; other projects were left unchanged.` : "Doctor overrides options for all projects at once; apply the change per project if they need different settings.";
|
|
391
|
+
log($.dim(` ${note}`));
|
|
392
|
+
}
|
|
393
|
+
log($.dim(" Trade-offs of every option: https://vitest.dev/guide/improving-performance"));
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The measured runs execute this script instead of the CLI binary: it accepts
|
|
397
|
+
* the overrides as JSON and can apply per-project changes (the environment
|
|
398
|
+
* swap) that CLI flags cannot express. `createVitest` attaches the projects,
|
|
399
|
+
* so the swap mutates each resolved project config before the run starts.
|
|
400
|
+
*/
|
|
401
|
+
function createRunnerScript() {
|
|
402
|
+
const nodeEntry = pathToFileURL(resolve(distDir, "node.js")).href;
|
|
403
|
+
return `import { createVitest } from ${JSON.stringify(nodeEntry)}
|
|
404
|
+
|
|
405
|
+
const { filters, options, envSwap } = JSON.parse(process.argv[2])
|
|
406
|
+
const ctx = await createVitest(options)
|
|
407
|
+
if (envSwap) {
|
|
408
|
+
for (const project of ctx.projects) {
|
|
409
|
+
if (!project.config.browser.enabled && project.config.environment === envSwap.from) {
|
|
410
|
+
project.config.environment = envSwap.to
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
await ctx.start(filters)
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
console.error(error?.stack || String(error))
|
|
419
|
+
process.exitCode = 1
|
|
420
|
+
}
|
|
421
|
+
await ctx.close()
|
|
422
|
+
process.exit(process.exitCode || 0)
|
|
423
|
+
`;
|
|
424
|
+
}
|
|
425
|
+
function logFailureExcerpt(title, stderr) {
|
|
426
|
+
log();
|
|
427
|
+
log(` ${$.red(title)}`);
|
|
428
|
+
if (!stderr.trim()) {
|
|
429
|
+
log($.dim(" (the run produced no error output - rerun with the same options to inspect it)"));
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const lines = stderr.trim().split("\n");
|
|
433
|
+
const excerpt = lines.slice(-15);
|
|
434
|
+
if (lines.length > excerpt.length) log($.dim(` … (last ${excerpt.length} lines)`));
|
|
435
|
+
for (const line of excerpt) log($.dim(` ${line}`));
|
|
436
|
+
}
|
|
437
|
+
function candidateNotes(result) {
|
|
438
|
+
switch (result.candidate.id) {
|
|
439
|
+
case "vmThreads":
|
|
440
|
+
case "vmForks": return [`vm pools keep per-file isolation but run test code in a VM context: cross-realm`, `instanceof edge cases and higher memory usage are possible (see vmMemoryLimit).`];
|
|
441
|
+
case "no-isolate":
|
|
442
|
+
case "threads-no-isolate": return [`The suite passed twice with a shuffled file order under shared state, so it is`, `likely - but not guaranteed - that no test depends on isolation.`];
|
|
443
|
+
case "happy-dom": return [
|
|
444
|
+
`The swap was applied only to projects running jsdom; other projects kept their`,
|
|
445
|
+
`environment. happy-dom implements the DOM differently than jsdom: the suite`,
|
|
446
|
+
`passed under it, but double-check tests that depend on layout, navigation or`,
|
|
447
|
+
`other DOM edge cases.`
|
|
448
|
+
];
|
|
449
|
+
case "fs-cache": return [`The fs module cache persists transformed modules on disk: repeated runs skip`, `the transforms, the first run after a file change still pays them.`];
|
|
450
|
+
default: return [];
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function describeProjects(projects) {
|
|
454
|
+
return `pool: ${[...new Set(projects.map((project) => project.browser ? "browser" : project.pool))].join(", ")} · isolate: ${projects.some((project) => project.isolate)}`;
|
|
455
|
+
}
|
|
456
|
+
function formatSeconds(time) {
|
|
457
|
+
return `${(time / 1e3).toFixed(2)}s`;
|
|
458
|
+
}
|
|
459
|
+
function formatDelta(wall, baseline) {
|
|
460
|
+
const delta = Math.round((wall - baseline) / baseline * 100);
|
|
461
|
+
if (delta === 0) return $.dim("(±0%)");
|
|
462
|
+
return delta < 0 ? $.green(`(${delta}%)`) : $.yellow(`(+${delta}%)`);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export { doctor, resolveDoctorCandidates };
|
|
@@ -4,5 +4,6 @@ const isNode = typeof process < "u" && typeof process.stdout < "u" && !process.v
|
|
|
4
4
|
const isDeno = typeof process < "u" && typeof process.stdout < "u" && process.versions?.deno !== void 0;
|
|
5
5
|
const isWindows = (isNode || isDeno) && process.platform === "win32";
|
|
6
6
|
const isTTY = (isNode || isDeno) && process.stdout?.isTTY && !isCI;
|
|
7
|
+
const isForceColor = () => "FORCE_COLOR" in process.env;
|
|
7
8
|
|
|
8
|
-
export {
|
|
9
|
+
export { isTTY as a, isWindows as b, isForceColor as i };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Awaitable } from '
|
|
1
|
+
import { A as Awaitable } from './config.d.B3YV4t0O.js';
|
|
2
2
|
|
|
3
3
|
interface EnvironmentReturn {
|
|
4
4
|
teardown: (global: any) => Awaitable<void>;
|
|
@@ -22,6 +22,14 @@ interface Environment {
|
|
|
22
22
|
* By default, fallbacks to `name`.
|
|
23
23
|
*/
|
|
24
24
|
viteEnvironment?: "client" | "ssr" | ({} & string);
|
|
25
|
+
/**
|
|
26
|
+
* Let the server transform the test file's import graph while `setupVM`
|
|
27
|
+
* runs. Only worth it when `setupVM` is slow (jsdom, happy-dom): otherwise
|
|
28
|
+
* the transforms compete with the worker's own module requests.
|
|
29
|
+
*
|
|
30
|
+
* @default `true`
|
|
31
|
+
*/
|
|
32
|
+
prewarmModules?: boolean;
|
|
25
33
|
setupVM?: (options: Record<string, any>) => Awaitable<VmEnvironmentReturn>;
|
|
26
34
|
setup: (global: any, options: Record<string, any>) => Awaitable<EnvironmentReturn>;
|
|
27
35
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { g as globalApis } from './constants.DLYM6cZY.js';
|
|
2
|
+
import { i as index } from './index.CZzC1gGg.js';
|
|
3
|
+
import './run.CjdgFZiW.js';
|
|
4
|
+
import './index.D0heQG4v.js';
|
|
5
|
+
import './tinyrainbow.Do8I1XRG.js';
|
|
6
|
+
import './display.BlTmubjj.js';
|
|
7
|
+
import './pathe.M-eThtNZ.zq3TM4NT.js';
|
|
8
|
+
import './source-map.DakdQc29.js';
|
|
9
|
+
import '../task-utils.js';
|
|
10
|
+
import './utils.DYj33du9.js';
|
|
11
|
+
import './spy.DDMXghXv.js';
|
|
12
|
+
import 'chai';
|
|
13
|
+
import './plugins.DPQHVT0p.js';
|
|
14
|
+
import './rawSnapshot.DqSiqIEx.js';
|
|
15
|
+
import './offset.Dy-5Fdfn.js';
|
|
16
|
+
import './rpc.Uhkwr2NM.js';
|
|
17
|
+
import 'vite/module-runner';
|
|
18
|
+
import './index.DmDMHCg8.js';
|
|
19
|
+
import 'tinybench';
|
|
20
|
+
import 'expect-type';
|
|
21
|
+
|
|
22
|
+
function registerApiGlobally() {
|
|
23
|
+
globalApis.forEach((api) => {
|
|
24
|
+
// @ts-expect-error I know what I am doing :P
|
|
25
|
+
globalThis[api] = index[api];
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { registerApiGlobally };
|