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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
import { processError } from '@vitest/utils/error';
import { parseSingleStack } from '@vitest/utils/source-map';
import { relative } from 'pathe';
import { toArray } from '@vitest/utils/helpers';
function createChainable(keys, fn) {
function create(context) {
const chain = function(...args) {
return fn.apply(context, args);
};
Object.assign(chain, fn);
chain.withContext = () => chain.bind(context);
chain.setContext = (key, value) => {
context[key] = value;
};
chain.mergeContext = (ctx) => {
Object.assign(context, ctx);
};
for (const key of keys) {
Object.defineProperty(chain, key, { get() {
return create({
...context,
[key]: true
});
} });
}
return chain;
}
const chain = create({});
chain.fn = fn;
return chain;
}
/**
* If any tasks been marked as `only`, mark all other tasks as `skip`.
*/
function interpretTaskModes(file, namePattern, testLocations, onlyMode, parentIsOnly, allowOnly) {
const matchedLocations = [];
const traverseSuite = (suite, parentIsOnly, parentMatchedWithLocation) => {
const suiteIsOnly = parentIsOnly || suite.mode === "only";
// Check if any tasks in this suite have `.only` - if so, only those should run
const hasSomeTasksOnly = onlyMode && suite.tasks.some((t) => t.mode === "only" || t.type === "suite" && someTasksAreOnly(t));
suite.tasks.forEach((t) => {
// Check if either the parent suite or the task itself are marked as included
// If there are tasks with `.only` in this suite, only include those (not all tasks from describe.only)
const includeTask = hasSomeTasksOnly ? t.mode === "only" || t.type === "suite" && someTasksAreOnly(t) : suiteIsOnly || t.mode === "only";
if (onlyMode) {
if (t.type === "suite" && (includeTask || someTasksAreOnly(t))) {
// Don't skip this suite
if (t.mode === "only") {
checkAllowOnly(t, allowOnly);
t.mode = "run";
}
} else if (t.mode === "run" && !includeTask) {
t.mode = "skip";
} else if (t.mode === "only") {
checkAllowOnly(t, allowOnly);
t.mode = "run";
}
}
let hasLocationMatch = parentMatchedWithLocation;
// Match test location against provided locations, only run if present
// in `testLocations`. Note: if `includeTaskLocations` is not enabled,
// all test will be skipped.
if (testLocations !== undefined && testLocations.length !== 0) {
if (t.location && (testLocations === null || testLocations === void 0 ? void 0 : testLocations.includes(t.location.line))) {
t.mode = "run";
matchedLocations.push(t.location.line);
hasLocationMatch = true;
} else if (parentMatchedWithLocation) {
t.mode = "run";
} else if (t.type === "test") {
t.mode = "skip";
}
}
if (t.type === "test") {
if (namePattern && !getTaskFullName(t).match(namePattern)) {
t.mode = "skip";
}
} else if (t.type === "suite") {
if (t.mode === "skip") {
skipAllTasks(t);
} else if (t.mode === "todo") {
todoAllTasks(t);
} else {
traverseSuite(t, includeTask, hasLocationMatch);
}
}
});
// if all subtasks are skipped, mark as skip
if (suite.mode === "run" || suite.mode === "queued") {
if (suite.tasks.length && suite.tasks.every((i) => i.mode !== "run" && i.mode !== "queued")) {
suite.mode = "skip";
}
}
};
traverseSuite(file, parentIsOnly, false);
const nonMatching = testLocations === null || testLocations === void 0 ? void 0 : testLocations.filter((loc) => !matchedLocations.includes(loc));
if (nonMatching && nonMatching.length !== 0) {
const message = nonMatching.length === 1 ? `line ${nonMatching[0]}` : `lines ${nonMatching.join(", ")}`;
if (file.result === undefined) {
file.result = {
state: "fail",
errors: []
};
}
if (file.result.errors === undefined) {
file.result.errors = [];
}
file.result.errors.push(processError(new Error(`No test found in ${file.name} in ${message}`)));
}
}
function getTaskFullName(task) {
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ""}${task.name}`;
}
function someTasksAreOnly(suite) {
return suite.tasks.some((t) => t.mode === "only" || t.type === "suite" && someTasksAreOnly(t));
}
function skipAllTasks(suite) {
suite.tasks.forEach((t) => {
if (t.mode === "run" || t.mode === "queued") {
t.mode = "skip";
if (t.type === "suite") {
skipAllTasks(t);
}
}
});
}
function todoAllTasks(suite) {
suite.tasks.forEach((t) => {
if (t.mode === "run" || t.mode === "queued") {
t.mode = "todo";
if (t.type === "suite") {
todoAllTasks(t);
}
}
});
}
function checkAllowOnly(task, allowOnly) {
if (allowOnly) {
return;
}
const error = processError(new Error("[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error"));
task.result = {
state: "fail",
errors: [error]
};
}
/* @__NO_SIDE_EFFECTS__ */
function generateHash(str) {
let hash = 0;
if (str.length === 0) {
return `${hash}`;
}
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return `${hash}`;
}
function calculateSuiteHash(parent) {
parent.tasks.forEach((t, idx) => {
t.id = `${parent.id}_${idx}`;
if (t.type === "suite") {
calculateSuiteHash(t);
}
});
}
function createFileTask(filepath, root, projectName, pool, viteEnvironment) {
const path = relative(root, filepath);
const file = {
id: generateFileHash(path, projectName),
name: path,
fullName: path,
type: "suite",
mode: "queued",
filepath,
tasks: [],
meta: Object.create(null),
projectName,
file: undefined,
pool,
viteEnvironment
};
file.file = file;
return file;
}
/**
* Generate a unique ID for a file based on its path and project name
* @param file File relative to the root of the project to keep ID the same between different machines
* @param projectName The name of the test project
*/
/* @__NO_SIDE_EFFECTS__ */
function generateFileHash(file, projectName) {
return /* @__PURE__ */ generateHash(`${file}${projectName || ""}`);
}
function findTestFileStackTrace(testFilePath, error) {
// first line is the error message
const lines = error.split("\n").slice(1);
for (const line of lines) {
const stack = parseSingleStack(line);
if (stack && stack.file === testFilePath) {
return stack;
}
}
}
/**
* Return a function for running multiple async operations with limited concurrency.
*/
function limitConcurrency(concurrency = Infinity) {
// The number of currently active + pending tasks.
let count = 0;
// The head and tail of the pending task queue, built using a singly linked list.
// Both head and tail are initially undefined, signifying an empty queue.
// They both become undefined again whenever there are no pending tasks.
let head;
let tail;
// A bookkeeping function executed whenever a task has been run to completion.
const finish = () => {
count--;
// Check if there are further pending tasks in the queue.
if (head) {
// Allow the next pending task to run and pop it from the queue.
head[0]();
head = head[1];
// The head may now be undefined if there are no further pending tasks.
// In that case, set tail to undefined as well.
tail = head && tail;
}
};
return (func, ...args) => {
// Create a promise chain that:
// 1. Waits for its turn in the task queue (if necessary).
// 2. Runs the task.
// 3. Allows the next pending task (if any) to run.
return new Promise((resolve) => {
if (count++ < concurrency) {
// No need to queue if fewer than maxConcurrency tasks are running.
resolve();
} else if (tail) {
// There are pending tasks, so append to the queue.
tail = tail[1] = [resolve];
} else {
// No other pending tasks, initialize the queue with a new tail and head.
head = tail = [resolve];
}
}).then(() => {
// Running func here ensures that even a non-thenable result or an
// immediately thrown error gets wrapped into a Promise.
return func(...args);
}).finally(finish);
};
}
/**
* Partition in tasks groups by consecutive concurrent
*/
function partitionSuiteChildren(suite) {
let tasksGroup = [];
const tasksGroups = [];
for (const c of suite.tasks) {
if (tasksGroup.length === 0 || c.concurrent === tasksGroup[0].concurrent) {
tasksGroup.push(c);
} else {
tasksGroups.push(tasksGroup);
tasksGroup = [c];
}
}
if (tasksGroup.length > 0) {
tasksGroups.push(tasksGroup);
}
return tasksGroups;
}
function isTestCase(s) {
return s.type === "test";
}
function getTests(suite) {
const tests = [];
const arraySuites = toArray(suite);
for (const s of arraySuites) {
if (isTestCase(s)) {
tests.push(s);
} else {
for (const task of s.tasks) {
if (isTestCase(task)) {
tests.push(task);
} else {
const taskTests = getTests(task);
for (const test of taskTests) {
tests.push(test);
}
}
}
}
}
return tests;
}
function getTasks(tasks = []) {
return toArray(tasks).flatMap((s) => isTestCase(s) ? [s] : [s, ...getTasks(s.tasks)]);
}
function getSuites(suite) {
return toArray(suite).flatMap((s) => s.type === "suite" ? [s, ...getSuites(s.tasks)] : []);
}
function hasTests(suite) {
return toArray(suite).some((s) => s.tasks.some((c) => isTestCase(c) || hasTests(c)));
}
function hasFailed(suite) {
return toArray(suite).some((s) => {
var _s$result;
return ((_s$result = s.result) === null || _s$result === void 0 ? void 0 : _s$result.state) === "fail" || s.type === "suite" && hasFailed(s.tasks);
});
}
function getNames(task) {
const names = [task.name];
let current = task;
while (current === null || current === void 0 ? void 0 : current.suite) {
current = current.suite;
if (current === null || current === void 0 ? void 0 : current.name) {
names.unshift(current.name);
}
}
if (current !== task.file) {
names.unshift(task.file.name);
}
return names;
}
function getFullName(task, separator = " > ") {
return getNames(task).join(separator);
}
function getTestName(task, separator = " > ") {
return getNames(task).slice(1).join(separator);
}
function createTaskName(names, separator = " > ") {
return names.filter((name) => name !== undefined).join(separator);
}
export { calculateSuiteHash as a, createFileTask as b, createChainable as c, generateHash as d, createTaskName as e, findTestFileStackTrace as f, generateFileHash as g, getFullName as h, interpretTaskModes as i, getNames as j, getSuites as k, limitConcurrency as l, getTasks as m, getTestName as n, getTests as o, partitionSuiteChildren as p, hasFailed as q, hasTests as r, someTasksAreOnly as s, isTestCase as t };
|