vitest 4.0.0-beta.1 → 4.0.0-beta.10
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 +2 -2
- package/dist/browser.d.ts +13 -14
- package/dist/browser.js +6 -5
- package/dist/chunks/base.Cjha6usc.js +129 -0
- package/dist/chunks/{benchmark.CYdenmiT.js → benchmark.CJUa-Hsa.js} +6 -8
- package/dist/chunks/{benchmark.d.BwvBVTda.d.ts → benchmark.d.DAaHLpsq.d.ts} +4 -4
- package/dist/chunks/{browser.d.q8Z0P0q1.d.ts → browser.d.yFAklsD1.d.ts} +5 -5
- package/dist/chunks/{cac.D3EzDDZd.js → cac.DCxo_nSu.js} +70 -152
- package/dist/chunks/{cli-api.Dn5gKePv.js → cli-api.BJJXh9BV.js} +1330 -1677
- package/dist/chunks/{config.d.HJdfX-8k.d.ts → config.d.B_LthbQq.d.ts} +58 -63
- package/dist/chunks/{console.CtFJOzRO.js → console.7h5kHUIf.js} +34 -70
- package/dist/chunks/{constants.DnKduX2e.js → constants.D_Q9UYh-.js} +1 -9
- package/dist/chunks/{coverage.Cwa-XhJt.js → coverage.BCU-r2QL.js} +515 -781
- package/dist/chunks/{coverage.DVF1vEu8.js → coverage.D_JHT54q.js} +2 -2
- package/dist/chunks/{coverage.d.S9RMNXIe.d.ts → coverage.d.BZtK59WP.d.ts} +10 -8
- package/dist/chunks/{creator.GK6I-cL4.js → creator.08Gi-vCA.js} +93 -77
- package/dist/chunks/{date.Bq6ZW5rf.js → date.-jtEtIeV.js} +6 -17
- package/dist/chunks/{environment.d.CUq4cUgQ.d.ts → environment.d.BsToaxti.d.ts} +27 -6
- package/dist/chunks/{git.BVQ8w_Sw.js → git.BFNcloKD.js} +1 -2
- package/dist/chunks/{global.d.CVbXEflG.d.ts → global.d.BK3X7FW1.d.ts} +2 -5
- package/dist/chunks/{globals.Cxal6MLI.js → globals.DG-S3xFe.js} +8 -8
- package/dist/chunks/{index.CZI_8rVt.js → index.BIP7prJq.js} +289 -608
- package/dist/chunks/{index.B521nVV-.js → index.Bgo3tNWt.js} +23 -4
- package/dist/chunks/{index.TfbsX-3I.js → index.BjKEiSn0.js} +14 -24
- package/dist/chunks/{index.BWf_gE5n.js → index.CMfqw92x.js} +7 -6
- package/dist/chunks/{index.CmSc2RE5.js → index.DIWhzsUh.js} +72 -118
- package/dist/chunks/{inspector.C914Efll.js → inspector.CvQD-Nie.js} +10 -25
- package/dist/chunks/moduleRunner.d.D9nBoC4p.d.ts +201 -0
- package/dist/chunks/moduleTransport.I-bgQy0S.js +19 -0
- package/dist/chunks/{node.fjCdwEIl.js → node.CyipiPvJ.js} +1 -1
- package/dist/chunks/{plugin.d.C2EcJUjo.d.ts → plugin.d.BMVSnsGV.d.ts} +1 -1
- package/dist/chunks/{reporters.d.DxZg19fy.d.ts → reporters.d.BUWjmRYq.d.ts} +1226 -1291
- package/dist/chunks/resolveSnapshotEnvironment.Bkht6Yor.js +81 -0
- package/dist/chunks/resolver.Bx6lE0iq.js +119 -0
- package/dist/chunks/rpc.BKr6mtxz.js +65 -0
- package/dist/chunks/{setup-common.D7ZqXFx-.js → setup-common.uiMcU3cv.js} +17 -29
- package/dist/chunks/startModuleRunner.p67gbNo9.js +665 -0
- package/dist/chunks/{suite.d.FvehnV49.d.ts → suite.d.BJWk38HB.d.ts} +1 -1
- package/dist/chunks/test.BiqSKISg.js +214 -0
- package/dist/chunks/{typechecker.CVytUJuF.js → typechecker.DB-fIMaH.js} +144 -213
- package/dist/chunks/{utils.CAioKnHs.js → utils.C2YI6McM.js} +5 -14
- package/dist/chunks/{utils.XdZDrNZV.js → utils.D2R2NiOH.js} +8 -27
- package/dist/chunks/{vi.bdSIJ99Y.js → vi.ZPgvtBao.js} +156 -305
- package/dist/chunks/{vm.BThCzidc.js → vm.Ca0Y0W5f.js} +116 -226
- package/dist/chunks/{worker.d.DoNjFAiv.d.ts → worker.d.BDsXGkwh.d.ts} +28 -22
- package/dist/chunks/{worker.d.CmvJfRGs.d.ts → worker.d.BNcX_2mH.d.ts} +1 -1
- package/dist/cli.js +4 -4
- package/dist/config.cjs +3 -9
- package/dist/config.d.ts +49 -54
- package/dist/config.js +1 -1
- package/dist/coverage.d.ts +27 -26
- package/dist/coverage.js +6 -7
- package/dist/environments.d.ts +9 -13
- package/dist/environments.js +1 -1
- package/dist/index.d.ts +38 -45
- package/dist/index.js +7 -9
- package/dist/module-evaluator.d.ts +13 -0
- package/dist/module-evaluator.js +276 -0
- package/dist/module-runner.js +15 -0
- package/dist/node.d.ts +40 -41
- package/dist/node.js +23 -33
- package/dist/reporters.d.ts +12 -13
- package/dist/reporters.js +3 -3
- package/dist/runners.d.ts +3 -3
- package/dist/runners.js +13 -232
- package/dist/snapshot.js +2 -2
- package/dist/suite.d.ts +2 -2
- package/dist/suite.js +2 -2
- package/dist/worker.js +90 -47
- package/dist/workers/forks.js +34 -10
- package/dist/workers/runVmTests.js +36 -56
- package/dist/workers/threads.js +34 -10
- package/dist/workers/vmForks.js +11 -10
- package/dist/workers/vmThreads.js +11 -10
- package/dist/workers.d.ts +5 -4
- package/dist/workers.js +35 -17
- package/globals.d.ts +17 -17
- package/package.json +32 -31
- package/dist/chunks/base.Bj3pWTr1.js +0 -38
- package/dist/chunks/execute.B7h3T_Hc.js +0 -708
- package/dist/chunks/index.D-VkfKhf.js +0 -105
- package/dist/chunks/rpc.CsFtxqeq.js +0 -83
- package/dist/chunks/runBaseTests.BC7ZIH5L.js +0 -129
- package/dist/execute.d.ts +0 -148
- package/dist/execute.js +0 -13
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { updateTask } from '@vitest/runner';
|
|
2
|
+
import { createDefer, getSafeTimers } from '@vitest/utils';
|
|
3
|
+
import { a as getBenchOptions, g as getBenchFn } from './benchmark.CJUa-Hsa.js';
|
|
4
|
+
import { g as getWorkerState } from './utils.D2R2NiOH.js';
|
|
5
|
+
import { setState, GLOBAL_EXPECT, getState } from '@vitest/expect';
|
|
6
|
+
import { getTests, getNames, getTestName } from '@vitest/runner/utils';
|
|
7
|
+
import { processError } from '@vitest/utils/error';
|
|
8
|
+
import { normalize } from 'pathe';
|
|
9
|
+
import { a as getSnapshotClient, i as inject, c as createExpect, v as vi } from './vi.ZPgvtBao.js';
|
|
10
|
+
import { r as rpc } from './rpc.BKr6mtxz.js';
|
|
11
|
+
|
|
12
|
+
function createBenchmarkResult(name) {
|
|
13
|
+
return {
|
|
14
|
+
name,
|
|
15
|
+
rank: 0,
|
|
16
|
+
rme: 0,
|
|
17
|
+
samples: []
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const benchmarkTasks = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
async function runBenchmarkSuite(suite, runner) {
|
|
22
|
+
const { Task, Bench } = await runner.importTinybench(), start = performance.now(), benchmarkGroup = [], benchmarkSuiteGroup = [];
|
|
23
|
+
for (const task of suite.tasks) {
|
|
24
|
+
if (task.mode !== "run" && task.mode !== "queued") continue;
|
|
25
|
+
if (task.meta?.benchmark) benchmarkGroup.push(task);
|
|
26
|
+
else if (task.type === "suite") benchmarkSuiteGroup.push(task);
|
|
27
|
+
}
|
|
28
|
+
// run sub suites sequentially
|
|
29
|
+
for (const subSuite of benchmarkSuiteGroup) await runBenchmarkSuite(subSuite, runner);
|
|
30
|
+
if (benchmarkGroup.length) {
|
|
31
|
+
const defer = createDefer();
|
|
32
|
+
suite.result = {
|
|
33
|
+
state: "run",
|
|
34
|
+
startTime: start,
|
|
35
|
+
benchmark: createBenchmarkResult(suite.name)
|
|
36
|
+
}, updateTask$1("suite-prepare", suite);
|
|
37
|
+
const addBenchTaskListener = (task, benchmark) => {
|
|
38
|
+
task.addEventListener("complete", (e) => {
|
|
39
|
+
const task = e.task, taskRes = task.result, result = benchmark.result.benchmark;
|
|
40
|
+
benchmark.result.state = "pass", Object.assign(result, taskRes);
|
|
41
|
+
// compute extra stats and free raw samples as early as possible
|
|
42
|
+
const samples = result.samples;
|
|
43
|
+
if (result.sampleCount = samples.length, result.median = samples.length % 2 ? samples[Math.floor(samples.length / 2)] : (samples[samples.length / 2] + samples[samples.length / 2 - 1]) / 2, !runner.config.benchmark?.includeSamples) result.samples.length = 0;
|
|
44
|
+
updateTask$1("test-finished", benchmark);
|
|
45
|
+
}, { once: true }), task.addEventListener("error", (e) => {
|
|
46
|
+
const task = e.task;
|
|
47
|
+
defer.reject(benchmark ? task.result.error : e);
|
|
48
|
+
}, { once: true });
|
|
49
|
+
};
|
|
50
|
+
benchmarkGroup.forEach((benchmark) => {
|
|
51
|
+
const options = getBenchOptions(benchmark), benchmarkInstance = new Bench(options), benchmarkFn = getBenchFn(benchmark);
|
|
52
|
+
benchmark.result = {
|
|
53
|
+
state: "run",
|
|
54
|
+
startTime: start,
|
|
55
|
+
benchmark: createBenchmarkResult(benchmark.name)
|
|
56
|
+
};
|
|
57
|
+
const task = new Task(benchmarkInstance, benchmark.name, benchmarkFn);
|
|
58
|
+
benchmarkTasks.set(benchmark, task), addBenchTaskListener(task, benchmark);
|
|
59
|
+
});
|
|
60
|
+
const { setTimeout } = getSafeTimers(), tasks = [];
|
|
61
|
+
for (const benchmark of benchmarkGroup) {
|
|
62
|
+
const task = benchmarkTasks.get(benchmark);
|
|
63
|
+
updateTask$1("test-prepare", benchmark), await task.warmup(), tasks.push([await new Promise((resolve) => setTimeout(async () => {
|
|
64
|
+
resolve(await task.run());
|
|
65
|
+
})), benchmark]);
|
|
66
|
+
}
|
|
67
|
+
suite.result.duration = performance.now() - start, suite.result.state = "pass", updateTask$1("suite-finished", suite), defer.resolve(null), await defer;
|
|
68
|
+
}
|
|
69
|
+
function updateTask$1(event, task) {
|
|
70
|
+
updateTask(event, task, runner);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
class NodeBenchmarkRunner {
|
|
74
|
+
moduleRunner;
|
|
75
|
+
constructor(config) {
|
|
76
|
+
this.config = config;
|
|
77
|
+
}
|
|
78
|
+
async importTinybench() {
|
|
79
|
+
return await import('tinybench');
|
|
80
|
+
}
|
|
81
|
+
importFile(filepath, source) {
|
|
82
|
+
if (source === "setup") {
|
|
83
|
+
const moduleNode = getWorkerState().evaluatedModules.getModuleById(filepath);
|
|
84
|
+
if (moduleNode) getWorkerState().evaluatedModules.invalidateModule(moduleNode);
|
|
85
|
+
}
|
|
86
|
+
return this.moduleRunner.import(filepath);
|
|
87
|
+
}
|
|
88
|
+
async runSuite(suite) {
|
|
89
|
+
await runBenchmarkSuite(suite, this);
|
|
90
|
+
}
|
|
91
|
+
async runTask() {
|
|
92
|
+
throw new Error("`test()` and `it()` is only available in test mode.");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// import type { VitestExecutor } from '../execute'
|
|
97
|
+
// worker context is shared between all tests
|
|
98
|
+
const workerContext = Object.create(null);
|
|
99
|
+
class VitestTestRunner {
|
|
100
|
+
snapshotClient = getSnapshotClient();
|
|
101
|
+
workerState = getWorkerState();
|
|
102
|
+
moduleRunner;
|
|
103
|
+
cancelRun = false;
|
|
104
|
+
assertionsErrors = /* @__PURE__ */ new WeakMap();
|
|
105
|
+
pool = this.workerState.ctx.pool;
|
|
106
|
+
constructor(config) {
|
|
107
|
+
this.config = config;
|
|
108
|
+
}
|
|
109
|
+
importFile(filepath, source) {
|
|
110
|
+
if (source === "setup") {
|
|
111
|
+
const moduleNode = this.workerState.evaluatedModules.getModuleById(filepath);
|
|
112
|
+
if (moduleNode) this.workerState.evaluatedModules.invalidateModule(moduleNode);
|
|
113
|
+
}
|
|
114
|
+
return this.moduleRunner.import(filepath);
|
|
115
|
+
}
|
|
116
|
+
onCollectStart(file) {
|
|
117
|
+
this.workerState.current = file;
|
|
118
|
+
}
|
|
119
|
+
onCleanupWorkerContext(listener) {
|
|
120
|
+
this.workerState.onCleanup(listener);
|
|
121
|
+
}
|
|
122
|
+
onAfterRunFiles() {
|
|
123
|
+
this.snapshotClient.clear(), this.workerState.current = void 0;
|
|
124
|
+
}
|
|
125
|
+
getWorkerContext() {
|
|
126
|
+
return workerContext;
|
|
127
|
+
}
|
|
128
|
+
async onAfterRunSuite(suite) {
|
|
129
|
+
if (this.config.logHeapUsage && typeof process !== "undefined") suite.result.heap = process.memoryUsage().heapUsed;
|
|
130
|
+
if (suite.mode !== "skip" && "filepath" in suite) {
|
|
131
|
+
// mark snapshots in skipped tests as not obsolete
|
|
132
|
+
for (const test of getTests(suite)) if (test.mode === "skip") {
|
|
133
|
+
const name = getNames(test).slice(1).join(" > ");
|
|
134
|
+
this.snapshotClient.skipTest(suite.file.filepath, name);
|
|
135
|
+
}
|
|
136
|
+
const result = await this.snapshotClient.finish(suite.file.filepath);
|
|
137
|
+
if (this.workerState.config.snapshotOptions.updateSnapshot === "none" && result.unchecked) {
|
|
138
|
+
let message = `Obsolete snapshots found when no snapshot update is expected.\n`;
|
|
139
|
+
for (const key of result.uncheckedKeys) message += `· ${key}\n`;
|
|
140
|
+
suite.result.errors ??= [], suite.result.errors.push(processError(new Error(message))), suite.result.state = "fail";
|
|
141
|
+
}
|
|
142
|
+
await rpc().snapshotSaved(result);
|
|
143
|
+
}
|
|
144
|
+
this.workerState.current = suite.suite || suite.file;
|
|
145
|
+
}
|
|
146
|
+
onAfterRunTask(test) {
|
|
147
|
+
if (this.config.logHeapUsage && typeof process !== "undefined") test.result.heap = process.memoryUsage().heapUsed;
|
|
148
|
+
this.workerState.current = test.suite || test.file;
|
|
149
|
+
}
|
|
150
|
+
cancel(_reason) {
|
|
151
|
+
this.cancelRun = true;
|
|
152
|
+
}
|
|
153
|
+
injectValue(key) {
|
|
154
|
+
// inject has a very limiting type controlled by ProvidedContext
|
|
155
|
+
// some tests override it which causes the build to fail
|
|
156
|
+
return inject(key);
|
|
157
|
+
}
|
|
158
|
+
async onBeforeRunTask(test) {
|
|
159
|
+
if (this.cancelRun) test.mode = "skip";
|
|
160
|
+
test.mode !== "run" && test.mode !== "queued" || (this.workerState.current = test);
|
|
161
|
+
}
|
|
162
|
+
async onBeforeRunSuite(suite) {
|
|
163
|
+
if (this.cancelRun) suite.mode = "skip";
|
|
164
|
+
// initialize snapshot state before running file suite
|
|
165
|
+
if (suite.mode !== "skip" && "filepath" in suite) await this.snapshotClient.setup(suite.file.filepath, this.workerState.config.snapshotOptions);
|
|
166
|
+
this.workerState.current = suite;
|
|
167
|
+
}
|
|
168
|
+
onBeforeTryTask(test) {
|
|
169
|
+
clearModuleMocks(this.config), this.snapshotClient.clearTest(test.file.filepath, test.id), setState({
|
|
170
|
+
assertionCalls: 0,
|
|
171
|
+
isExpectingAssertions: false,
|
|
172
|
+
isExpectingAssertionsError: null,
|
|
173
|
+
expectedAssertionsNumber: null,
|
|
174
|
+
expectedAssertionsNumberErrorGen: null,
|
|
175
|
+
currentTestName: getTestName(test),
|
|
176
|
+
snapshotState: this.snapshotClient.getSnapshotState(test.file.filepath)
|
|
177
|
+
}, globalThis[GLOBAL_EXPECT]);
|
|
178
|
+
}
|
|
179
|
+
onAfterTryTask(test) {
|
|
180
|
+
const { assertionCalls, expectedAssertionsNumber, expectedAssertionsNumberErrorGen, isExpectingAssertions, isExpectingAssertionsError } = test.context._local ? test.context.expect.getState() : getState(globalThis[GLOBAL_EXPECT]);
|
|
181
|
+
if (expectedAssertionsNumber !== null && assertionCalls !== expectedAssertionsNumber) throw expectedAssertionsNumberErrorGen();
|
|
182
|
+
if (isExpectingAssertions === true && assertionCalls === 0) throw isExpectingAssertionsError;
|
|
183
|
+
if (this.config.expect.requireAssertions && assertionCalls === 0) throw this.assertionsErrors.get(test);
|
|
184
|
+
}
|
|
185
|
+
extendTaskContext(context) {
|
|
186
|
+
// create error during the test initialization so we have a nice stack trace
|
|
187
|
+
if (this.config.expect.requireAssertions) this.assertionsErrors.set(context.task, /* @__PURE__ */ new Error("expected any number of assertion, but got none"));
|
|
188
|
+
let _expect;
|
|
189
|
+
return Object.defineProperty(context, "expect", { get() {
|
|
190
|
+
if (!_expect) _expect = createExpect(context.task);
|
|
191
|
+
return _expect;
|
|
192
|
+
} }), Object.defineProperty(context, "_local", { get() {
|
|
193
|
+
return _expect != null;
|
|
194
|
+
} }), context;
|
|
195
|
+
}
|
|
196
|
+
getImportDurations() {
|
|
197
|
+
const entries = [...this.workerState.moduleExecutionInfo?.entries() ?? []];
|
|
198
|
+
return Object.fromEntries(entries.map(([filepath, { duration, selfTime }]) => [normalize(filepath), {
|
|
199
|
+
selfTime,
|
|
200
|
+
totalTime: duration
|
|
201
|
+
}]));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function clearModuleMocks(config) {
|
|
205
|
+
const { clearMocks, mockReset, restoreMocks, unstubEnvs, unstubGlobals } = config;
|
|
206
|
+
// since each function calls another, we can just call one
|
|
207
|
+
if (restoreMocks) vi.restoreAllMocks();
|
|
208
|
+
else if (mockReset) vi.resetAllMocks();
|
|
209
|
+
else if (clearMocks) vi.clearAllMocks();
|
|
210
|
+
if (unstubEnvs) vi.unstubAllEnvs();
|
|
211
|
+
if (unstubGlobals) vi.unstubAllGlobals();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export { NodeBenchmarkRunner as N, VitestTestRunner as V };
|