@vitest/runner 3.2.4 → 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 +1 -1
- package/dist/chunk-tasks.js +332 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +1928 -3
- package/dist/{tasks.d-CkscK4of.d.ts → tasks.d-Mq4HCGzK.d.ts} +35 -53
- package/dist/types.d.ts +12 -12
- package/dist/utils.d.ts +5 -8
- package/dist/utils.js +3 -4
- package/package.json +2 -2
- package/dist/chunk-hooks.js +0 -2254
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2021-Present Vitest
|
|
3
|
+
Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { processError } from '@vitest/utils/error';
|
|
2
|
+
import { parseSingleStack } from '@vitest/utils/source-map';
|
|
3
|
+
import { relative } from 'pathe';
|
|
4
|
+
import { toArray } from '@vitest/utils';
|
|
5
|
+
|
|
6
|
+
function createChainable(keys, fn) {
|
|
7
|
+
function create(context) {
|
|
8
|
+
const chain = function(...args) {
|
|
9
|
+
return fn.apply(context, args);
|
|
10
|
+
};
|
|
11
|
+
Object.assign(chain, fn);
|
|
12
|
+
chain.withContext = () => chain.bind(context);
|
|
13
|
+
chain.setContext = (key, value) => {
|
|
14
|
+
context[key] = value;
|
|
15
|
+
};
|
|
16
|
+
chain.mergeContext = (ctx) => {
|
|
17
|
+
Object.assign(context, ctx);
|
|
18
|
+
};
|
|
19
|
+
for (const key of keys) {
|
|
20
|
+
Object.defineProperty(chain, key, { get() {
|
|
21
|
+
return create({
|
|
22
|
+
...context,
|
|
23
|
+
[key]: true
|
|
24
|
+
});
|
|
25
|
+
} });
|
|
26
|
+
}
|
|
27
|
+
return chain;
|
|
28
|
+
}
|
|
29
|
+
const chain = create({});
|
|
30
|
+
chain.fn = fn;
|
|
31
|
+
return chain;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* If any tasks been marked as `only`, mark all other tasks as `skip`.
|
|
36
|
+
*/
|
|
37
|
+
function interpretTaskModes(file, namePattern, testLocations, onlyMode, parentIsOnly, allowOnly) {
|
|
38
|
+
const matchedLocations = [];
|
|
39
|
+
const traverseSuite = (suite, parentIsOnly, parentMatchedWithLocation) => {
|
|
40
|
+
const suiteIsOnly = parentIsOnly || suite.mode === "only";
|
|
41
|
+
suite.tasks.forEach((t) => {
|
|
42
|
+
// Check if either the parent suite or the task itself are marked as included
|
|
43
|
+
const includeTask = suiteIsOnly || t.mode === "only";
|
|
44
|
+
if (onlyMode) {
|
|
45
|
+
if (t.type === "suite" && (includeTask || someTasksAreOnly(t))) {
|
|
46
|
+
// Don't skip this suite
|
|
47
|
+
if (t.mode === "only") {
|
|
48
|
+
checkAllowOnly(t, allowOnly);
|
|
49
|
+
t.mode = "run";
|
|
50
|
+
}
|
|
51
|
+
} else if (t.mode === "run" && !includeTask) {
|
|
52
|
+
t.mode = "skip";
|
|
53
|
+
} else if (t.mode === "only") {
|
|
54
|
+
checkAllowOnly(t, allowOnly);
|
|
55
|
+
t.mode = "run";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
let hasLocationMatch = parentMatchedWithLocation;
|
|
59
|
+
// Match test location against provided locations, only run if present
|
|
60
|
+
// in `testLocations`. Note: if `includeTaskLocations` is not enabled,
|
|
61
|
+
// all test will be skipped.
|
|
62
|
+
if (testLocations !== undefined && testLocations.length !== 0) {
|
|
63
|
+
if (t.location && (testLocations === null || testLocations === void 0 ? void 0 : testLocations.includes(t.location.line))) {
|
|
64
|
+
t.mode = "run";
|
|
65
|
+
matchedLocations.push(t.location.line);
|
|
66
|
+
hasLocationMatch = true;
|
|
67
|
+
} else if (parentMatchedWithLocation) {
|
|
68
|
+
t.mode = "run";
|
|
69
|
+
} else if (t.type === "test") {
|
|
70
|
+
t.mode = "skip";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (t.type === "test") {
|
|
74
|
+
if (namePattern && !getTaskFullName(t).match(namePattern)) {
|
|
75
|
+
t.mode = "skip";
|
|
76
|
+
}
|
|
77
|
+
} else if (t.type === "suite") {
|
|
78
|
+
if (t.mode === "skip") {
|
|
79
|
+
skipAllTasks(t);
|
|
80
|
+
} else if (t.mode === "todo") {
|
|
81
|
+
todoAllTasks(t);
|
|
82
|
+
} else {
|
|
83
|
+
traverseSuite(t, includeTask, hasLocationMatch);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
// if all subtasks are skipped, mark as skip
|
|
88
|
+
if (suite.mode === "run" || suite.mode === "queued") {
|
|
89
|
+
if (suite.tasks.length && suite.tasks.every((i) => i.mode !== "run" && i.mode !== "queued")) {
|
|
90
|
+
suite.mode = "skip";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
traverseSuite(file, parentIsOnly, false);
|
|
95
|
+
const nonMatching = testLocations === null || testLocations === void 0 ? void 0 : testLocations.filter((loc) => !matchedLocations.includes(loc));
|
|
96
|
+
if (nonMatching && nonMatching.length !== 0) {
|
|
97
|
+
const message = nonMatching.length === 1 ? `line ${nonMatching[0]}` : `lines ${nonMatching.join(", ")}`;
|
|
98
|
+
if (file.result === undefined) {
|
|
99
|
+
file.result = {
|
|
100
|
+
state: "fail",
|
|
101
|
+
errors: []
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (file.result.errors === undefined) {
|
|
105
|
+
file.result.errors = [];
|
|
106
|
+
}
|
|
107
|
+
file.result.errors.push(processError(new Error(`No test found in ${file.name} in ${message}`)));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function getTaskFullName(task) {
|
|
111
|
+
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ""}${task.name}`;
|
|
112
|
+
}
|
|
113
|
+
function someTasksAreOnly(suite) {
|
|
114
|
+
return suite.tasks.some((t) => t.mode === "only" || t.type === "suite" && someTasksAreOnly(t));
|
|
115
|
+
}
|
|
116
|
+
function skipAllTasks(suite) {
|
|
117
|
+
suite.tasks.forEach((t) => {
|
|
118
|
+
if (t.mode === "run" || t.mode === "queued") {
|
|
119
|
+
t.mode = "skip";
|
|
120
|
+
if (t.type === "suite") {
|
|
121
|
+
skipAllTasks(t);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function todoAllTasks(suite) {
|
|
127
|
+
suite.tasks.forEach((t) => {
|
|
128
|
+
if (t.mode === "run" || t.mode === "queued") {
|
|
129
|
+
t.mode = "todo";
|
|
130
|
+
if (t.type === "suite") {
|
|
131
|
+
todoAllTasks(t);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function checkAllowOnly(task, allowOnly) {
|
|
137
|
+
if (allowOnly) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const error = processError(new Error("[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error"));
|
|
141
|
+
task.result = {
|
|
142
|
+
state: "fail",
|
|
143
|
+
errors: [error]
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
147
|
+
function generateHash(str) {
|
|
148
|
+
let hash = 0;
|
|
149
|
+
if (str.length === 0) {
|
|
150
|
+
return `${hash}`;
|
|
151
|
+
}
|
|
152
|
+
for (let i = 0; i < str.length; i++) {
|
|
153
|
+
const char = str.charCodeAt(i);
|
|
154
|
+
hash = (hash << 5) - hash + char;
|
|
155
|
+
hash = hash & hash;
|
|
156
|
+
}
|
|
157
|
+
return `${hash}`;
|
|
158
|
+
}
|
|
159
|
+
function calculateSuiteHash(parent) {
|
|
160
|
+
parent.tasks.forEach((t, idx) => {
|
|
161
|
+
t.id = `${parent.id}_${idx}`;
|
|
162
|
+
if (t.type === "suite") {
|
|
163
|
+
calculateSuiteHash(t);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function createFileTask(filepath, root, projectName, pool) {
|
|
168
|
+
const path = relative(root, filepath);
|
|
169
|
+
const file = {
|
|
170
|
+
id: generateFileHash(path, projectName),
|
|
171
|
+
name: path,
|
|
172
|
+
type: "suite",
|
|
173
|
+
mode: "queued",
|
|
174
|
+
filepath,
|
|
175
|
+
tasks: [],
|
|
176
|
+
meta: Object.create(null),
|
|
177
|
+
projectName,
|
|
178
|
+
file: undefined,
|
|
179
|
+
pool
|
|
180
|
+
};
|
|
181
|
+
file.file = file;
|
|
182
|
+
return file;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Generate a unique ID for a file based on its path and project name
|
|
186
|
+
* @param file File relative to the root of the project to keep ID the same between different machines
|
|
187
|
+
* @param projectName The name of the test project
|
|
188
|
+
*/
|
|
189
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
190
|
+
function generateFileHash(file, projectName) {
|
|
191
|
+
return /* @__PURE__ */ generateHash(`${file}${projectName || ""}`);
|
|
192
|
+
}
|
|
193
|
+
function findTestFileStackTrace(testFilePath, error) {
|
|
194
|
+
// first line is the error message
|
|
195
|
+
const lines = error.split("\n").slice(1);
|
|
196
|
+
for (const line of lines) {
|
|
197
|
+
const stack = parseSingleStack(line);
|
|
198
|
+
if (stack && stack.file === testFilePath) {
|
|
199
|
+
return stack;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Return a function for running multiple async operations with limited concurrency.
|
|
206
|
+
*/
|
|
207
|
+
function limitConcurrency(concurrency = Infinity) {
|
|
208
|
+
// The number of currently active + pending tasks.
|
|
209
|
+
let count = 0;
|
|
210
|
+
// The head and tail of the pending task queue, built using a singly linked list.
|
|
211
|
+
// Both head and tail are initially undefined, signifying an empty queue.
|
|
212
|
+
// They both become undefined again whenever there are no pending tasks.
|
|
213
|
+
let head;
|
|
214
|
+
let tail;
|
|
215
|
+
// A bookkeeping function executed whenever a task has been run to completion.
|
|
216
|
+
const finish = () => {
|
|
217
|
+
count--;
|
|
218
|
+
// Check if there are further pending tasks in the queue.
|
|
219
|
+
if (head) {
|
|
220
|
+
// Allow the next pending task to run and pop it from the queue.
|
|
221
|
+
head[0]();
|
|
222
|
+
head = head[1];
|
|
223
|
+
// The head may now be undefined if there are no further pending tasks.
|
|
224
|
+
// In that case, set tail to undefined as well.
|
|
225
|
+
tail = head && tail;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return (func, ...args) => {
|
|
229
|
+
// Create a promise chain that:
|
|
230
|
+
// 1. Waits for its turn in the task queue (if necessary).
|
|
231
|
+
// 2. Runs the task.
|
|
232
|
+
// 3. Allows the next pending task (if any) to run.
|
|
233
|
+
return new Promise((resolve) => {
|
|
234
|
+
if (count++ < concurrency) {
|
|
235
|
+
// No need to queue if fewer than maxConcurrency tasks are running.
|
|
236
|
+
resolve();
|
|
237
|
+
} else if (tail) {
|
|
238
|
+
// There are pending tasks, so append to the queue.
|
|
239
|
+
tail = tail[1] = [resolve];
|
|
240
|
+
} else {
|
|
241
|
+
// No other pending tasks, initialize the queue with a new tail and head.
|
|
242
|
+
head = tail = [resolve];
|
|
243
|
+
}
|
|
244
|
+
}).then(() => {
|
|
245
|
+
// Running func here ensures that even a non-thenable result or an
|
|
246
|
+
// immediately thrown error gets wrapped into a Promise.
|
|
247
|
+
return func(...args);
|
|
248
|
+
}).finally(finish);
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Partition in tasks groups by consecutive concurrent
|
|
254
|
+
*/
|
|
255
|
+
function partitionSuiteChildren(suite) {
|
|
256
|
+
let tasksGroup = [];
|
|
257
|
+
const tasksGroups = [];
|
|
258
|
+
for (const c of suite.tasks) {
|
|
259
|
+
if (tasksGroup.length === 0 || c.concurrent === tasksGroup[0].concurrent) {
|
|
260
|
+
tasksGroup.push(c);
|
|
261
|
+
} else {
|
|
262
|
+
tasksGroups.push(tasksGroup);
|
|
263
|
+
tasksGroup = [c];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (tasksGroup.length > 0) {
|
|
267
|
+
tasksGroups.push(tasksGroup);
|
|
268
|
+
}
|
|
269
|
+
return tasksGroups;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function isTestCase(s) {
|
|
273
|
+
return s.type === "test";
|
|
274
|
+
}
|
|
275
|
+
function getTests(suite) {
|
|
276
|
+
const tests = [];
|
|
277
|
+
const arraySuites = toArray(suite);
|
|
278
|
+
for (const s of arraySuites) {
|
|
279
|
+
if (isTestCase(s)) {
|
|
280
|
+
tests.push(s);
|
|
281
|
+
} else {
|
|
282
|
+
for (const task of s.tasks) {
|
|
283
|
+
if (isTestCase(task)) {
|
|
284
|
+
tests.push(task);
|
|
285
|
+
} else {
|
|
286
|
+
const taskTests = getTests(task);
|
|
287
|
+
for (const test of taskTests) {
|
|
288
|
+
tests.push(test);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return tests;
|
|
295
|
+
}
|
|
296
|
+
function getTasks(tasks = []) {
|
|
297
|
+
return toArray(tasks).flatMap((s) => isTestCase(s) ? [s] : [s, ...getTasks(s.tasks)]);
|
|
298
|
+
}
|
|
299
|
+
function getSuites(suite) {
|
|
300
|
+
return toArray(suite).flatMap((s) => s.type === "suite" ? [s, ...getSuites(s.tasks)] : []);
|
|
301
|
+
}
|
|
302
|
+
function hasTests(suite) {
|
|
303
|
+
return toArray(suite).some((s) => s.tasks.some((c) => isTestCase(c) || hasTests(c)));
|
|
304
|
+
}
|
|
305
|
+
function hasFailed(suite) {
|
|
306
|
+
return toArray(suite).some((s) => {
|
|
307
|
+
var _s$result;
|
|
308
|
+
return ((_s$result = s.result) === null || _s$result === void 0 ? void 0 : _s$result.state) === "fail" || s.type === "suite" && hasFailed(s.tasks);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
function getNames(task) {
|
|
312
|
+
const names = [task.name];
|
|
313
|
+
let current = task;
|
|
314
|
+
while (current === null || current === void 0 ? void 0 : current.suite) {
|
|
315
|
+
current = current.suite;
|
|
316
|
+
if (current === null || current === void 0 ? void 0 : current.name) {
|
|
317
|
+
names.unshift(current.name);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (current !== task.file) {
|
|
321
|
+
names.unshift(task.file.name);
|
|
322
|
+
}
|
|
323
|
+
return names;
|
|
324
|
+
}
|
|
325
|
+
function getFullName(task, separator = " > ") {
|
|
326
|
+
return getNames(task).join(separator);
|
|
327
|
+
}
|
|
328
|
+
function getTestName(task, separator = " > ") {
|
|
329
|
+
return getNames(task).slice(1).join(separator);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export { calculateSuiteHash as a, createFileTask as b, createChainable as c, generateHash as d, getFullName as e, findTestFileStackTrace as f, generateFileHash as g, getNames as h, interpretTaskModes as i, getSuites as j, getTasks as k, limitConcurrency as l, getTestName as m, getTests as n, hasFailed as o, partitionSuiteChildren as p, hasTests as q, isTestCase as r, someTasksAreOnly as s };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as AfterAllListener, b as AfterEachListener, B as BeforeAllListener, d as BeforeEachListener, e as TaskHook, O as OnTestFailedHandler, f as OnTestFinishedHandler, a as Test,
|
|
2
|
-
export {
|
|
1
|
+
import { A as AfterAllListener, b as AfterEachListener, B as BeforeAllListener, d as BeforeEachListener, e as TaskHook, O as OnTestFailedHandler, f as OnTestFinishedHandler, a as Test, S as Suite, g as SuiteHooks, F as File, h as TaskUpdateEvent, T as Task, i as TestAPI, j as SuiteAPI, k as SuiteCollector } from './tasks.d-Mq4HCGzK.js';
|
|
2
|
+
export { l as Fixture, m as FixtureFn, n as FixtureOptions, o as Fixtures, H as HookCleanupCallback, p as HookListener, I as ImportDuration, q as InferFixturesTypes, R as RunMode, r as RuntimeContext, s as SequenceHooks, t as SequenceSetupFiles, u as SuiteFactory, v as TaskBase, w as TaskCustomOptions, x as TaskEventPack, y as TaskMeta, z as TaskPopulated, D as TaskResult, E as TaskResultPack, G as TaskState, J as TestAnnotation, K as TestAnnotationLocation, L as TestAttachment, M as TestContext, N as TestFunction, P as TestOptions, U as Use } from './tasks.d-Mq4HCGzK.js';
|
|
3
3
|
import { Awaitable } from '@vitest/utils';
|
|
4
4
|
import { FileSpecification, VitestRunner } from './types.js';
|
|
5
5
|
export { CancelReason, VitestRunnerConfig, VitestRunnerConstructor, VitestRunnerImportSource } from './types.js';
|
|
@@ -122,8 +122,8 @@ declare const onTestFailed: TaskHook<OnTestFailedHandler>;
|
|
|
122
122
|
*/
|
|
123
123
|
declare const onTestFinished: TaskHook<OnTestFinishedHandler>;
|
|
124
124
|
|
|
125
|
-
declare function setFn(key: Test
|
|
126
|
-
declare function getFn<Task = Test
|
|
125
|
+
declare function setFn(key: Test, fn: () => Awaitable<void>): void;
|
|
126
|
+
declare function getFn<Task = Test>(key: Task): () => Awaitable<void>;
|
|
127
127
|
declare function setHooks(key: Suite, hooks: SuiteHooks): void;
|
|
128
128
|
declare function getHooks(key: Suite): SuiteHooks;
|
|
129
129
|
|
|
@@ -258,4 +258,4 @@ declare function createTaskCollector(fn: (...args: any[]) => any, context?: Reco
|
|
|
258
258
|
|
|
259
259
|
declare function getCurrentTest<T extends Test | undefined>(): T;
|
|
260
260
|
|
|
261
|
-
export { AfterAllListener, AfterEachListener, BeforeAllListener, BeforeEachListener,
|
|
261
|
+
export { AfterAllListener, AfterEachListener, BeforeAllListener, BeforeEachListener, File, FileSpecification, OnTestFailedHandler, OnTestFinishedHandler, Suite, SuiteAPI, SuiteCollector, SuiteHooks, Task, TaskHook, TaskUpdateEvent, Test, TestAPI, VitestRunner, afterAll, afterEach, beforeAll, beforeEach, publicCollect as collectTests, createTaskCollector, describe, getCurrentSuite, getCurrentTest, getFn, getHooks, it, onTestFailed, onTestFinished, setFn, setHooks, startTests, suite, test, updateTask };
|