/
worker-runner.js
169 lines (151 loc) 路 4.31 KB
/
worker-runner.js
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
import path from "path";
import * as circus from "jest-circus";
import { pathToFileURL } from "url";
import "./global-setup.js";
/** @typedef {import("@jest/test-result").Test} Test */
// Node.js workers (worker_therads) don't support
// process.chdir, that we use multiple times in our tests.
// We can "polyfill" it for process.cwd() usage, but it
// won't affect path.* and fs.* functions.
{
const startCwd = process.cwd();
let cwd = startCwd;
process.cwd = () => cwd;
process.chdir = dir => {
cwd = path.resolve(cwd, dir);
};
}
export default async function ({ test, port }) {
port.postMessage("start");
const stats = { passes: 0, failures: 0, pending: 0 };
const results = [];
const { tests, hasFocusedTests } = await loadTests(test.path);
await runTestBlock(tests, hasFocusedTests, results, stats);
return toTestResult(stats, results, test.path);
}
async function loadTests(testFile) {
circus.resetState();
await import(pathToFileURL(testFile));
const { rootDescribeBlock, hasFocusedTests } = circus.getState();
return { tests: rootDescribeBlock, hasFocusedTests };
}
async function runTestBlock(
block,
hasFocusedTests,
results,
stats,
ancestors = []
) {
await runHooks("beforeAll", block, results, stats, ancestors);
for (const child of block.children) {
const { type, mode, fn, name } = child;
const nextAncestors = ancestors.concat(name);
if (
mode === "skip" ||
(hasFocusedTests && type === "test" && mode !== "only")
) {
stats.pending++;
results.push({ ancestors, title: name, error: null, skipped: true });
} else if (type === "describeBlock") {
await runTestBlock(child, hasFocusedTests, results, stats, nextAncestors);
} else if (type === "test") {
await runHooks("beforeEach", block, results, stats, nextAncestors, true);
await callAsync(fn).then(
() => {
stats.passes++;
results.push({ ancestors, title: name, error: null, skipped: false });
},
error => {
stats.failures++;
results.push({ ancestors, title: name, error, skipped: false });
}
);
await runHooks("afterEach", block, results, stats, nextAncestors, true);
}
}
await runHooks("afterAll", block, results, stats, ancestors);
return results;
}
async function runHooks(hook, block, results, stats, ancestors, runInParents) {
for (const { type, fn } of block.hooks) {
if (type === hook) {
await callAsync(fn).catch(error => {
stats.failures++;
results.push({ ancestors, title: `(${hook})`, error, skipped: false });
});
}
}
if (block.parent && runInParents) {
await runHooks(hook, block.parent, results, stats, ancestors, true);
}
}
function callAsync(fn) {
if (fn.length >= 1) {
return new Promise((resolve, reject) => {
fn((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
} else {
return Promise.resolve().then(fn);
}
}
/**
*
* @param {*} stats
* @param {*} tests
* @param {*} path
* @returns {import("@jest/test-result").TestResult}
*/
function toTestResult(stats, tests, path) {
return {
coverage: globalThis.__coverage__,
console: null,
failureMessage: tests
.filter(t => t.error)
.map(failureToString)
.join("\n"),
numFailingTests: stats.failures,
numPassingTests: stats.passes,
numPendingTests: stats.pending,
perfStats: {
end: +new Date(stats.end),
start: +new Date(stats.start),
},
skipped: false,
snapshot: {
added: 0,
fileDeleted: false,
matched: 0,
unchecked: 0,
unmatched: 0,
updated: 0,
},
sourceMaps: {},
testExecError: null,
testFilePath: path,
testResults: tests.map(test => {
return {
ancestorTitles: test.ancestors,
duration: test.duration / 1000,
failureMessages: test.error ? [failureToString(test)] : [],
fullName: test.title,
numPassingAsserts: test.error ? 1 : 0,
status: test.skipped ? "pending" : test.error ? "failed" : "passed",
title: test.title,
};
}),
};
}
function failureToString(test) {
return (
test.ancestors.concat(test.title).join(" > ") +
"\n" +
test.error.toString().replace(/^/gm, "\t") +
"\n"
);
}