-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrun.ts
163 lines (143 loc) · 4.96 KB
/
run.ts
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
import type { HooksCallback, ModuleInfo, TestContext, TestInfo } from '../-types';
import type { ModuleReport, TestReport } from '../-types/report';
import { getChain } from '../-utils';
import { Config, groupLogs, instrument } from './config';
import { DelegatingReporter } from './delegating-reporter';
import { Diagnostic } from './diagnostic';
export const PublicTestInfo = Symbol('TestInfo');
export async function runTest<TC extends TestContext>(
moduleReport: ModuleReport,
beforeChain: HooksCallback<TC>[],
test: TestInfo<TC>,
afterChain: HooksCallback<TC>[]
) {
const testContext = {
[PublicTestInfo]: {
id: test.id,
name: test.testName,
},
} as unknown as TC;
const testReport: TestReport = {
id: test.id,
name: test.name,
skipped: test.skip,
todo: test.todo,
start: null,
end: null,
measure: null,
result: {
diagnostics: [],
passed: true,
failed: false,
},
module: moduleReport,
};
testReport.start = instrument() && performance.mark(`test:${test.module.moduleName} > ${test.name}:start`);
const Assert = new Diagnostic(DelegatingReporter, Config, test, testReport);
groupLogs() && console.groupCollapsed(test.name);
DelegatingReporter.onTestStart(testReport);
if (test.skip) {
groupLogs() && console.groupEnd();
testReport.end = instrument() && performance.mark(`test:${test.module.moduleName} > ${test.name}:end`);
testReport.measure =
instrument() &&
performance.measure(`test:${test.module.moduleName} > ${test.name}`, testReport.start.name, testReport.end.name);
DelegatingReporter.onTestFinish(testReport);
return;
}
for (const hook of beforeChain) {
await hook.call(testContext, Assert);
}
try {
await test.cb.call(testContext, Assert);
} catch (err) {
Assert.pushResult({
message: `Unexpected Test Failure: ${(err as Error).message}`,
stack: (err as Error).stack!,
passed: false,
actual: false,
expected: true,
});
if (!Config.params.tryCatch.value) {
throw err;
}
}
for (const hook of afterChain) {
await hook.call(testContext, Assert);
}
Assert._finalize();
groupLogs() && console.groupEnd();
testReport.end = instrument() && performance.mark(`test:${test.module.moduleName} > ${test.name}:end`);
testReport.measure =
instrument() &&
performance.measure(`test:${test.module.moduleName} > ${test.name}`, testReport.start.name, testReport.end.name);
DelegatingReporter.onTestFinish(testReport);
}
export async function runModule<TC extends TestContext>(
module: ModuleInfo<TC>,
parents: ModuleInfo<TC>[] | null,
promises: Promise<void>[]
) {
groupLogs() && console.groupCollapsed(module.name);
const moduleReport: ModuleReport = {
name: module.moduleName,
start: null,
end: null,
measure: null,
passed: true,
failed: false,
};
moduleReport.start = instrument() && performance.mark(`module:${module.moduleName}:start`);
DelegatingReporter.onModuleStart(moduleReport);
for (const hook of Config.globalHooks.beforeModule) {
await hook();
}
for (const hook of module.config.beforeModule) {
await hook();
}
// run tests
const beforeChain = getChain<TC>(Config.globalHooks, module, parents, 'beforeEach');
const afterChain = getChain<TC>(Config.globalHooks, module, parents, 'afterEach');
if (Config.params.concurrency.value && Config.concurrency > 1) {
const tests = module.tests.byOrder;
let remainingTests = tests.length;
let currentTest = 0;
// once remaining tests is 0, we move on
// to the next module after at least one race has completed
while (remainingTests > 0) {
const needed = Config.concurrency - promises.length;
const available = Math.min(needed, remainingTests, Config.concurrency);
for (let i = 0; i < available; i++) {
const test = tests[currentTest++];
remainingTests--;
const promise = runTest(moduleReport, beforeChain, test, afterChain).finally(() => {
const index = promises.indexOf(promise);
void promises.splice(index, 1);
});
promises.push(promise);
}
if (promises.length === Config.concurrency) {
await Promise.race(promises);
}
}
} else {
for (const test of module.tests.byOrder) {
await runTest(moduleReport, beforeChain, test, afterChain);
}
}
// run modules
for (const childModule of module.modules.byOrder) {
await runModule(childModule, [...(parents || []), module], promises);
}
for (const hook of module.config.afterModule) {
await hook();
}
for (const hook of Config.globalHooks.afterModule) {
await hook();
}
groupLogs() && console.groupEnd();
moduleReport.end = instrument() && performance.mark(`module:${module.moduleName}:end`);
moduleReport.measure =
instrument() && performance.measure(`module:${module.moduleName}`, moduleReport.start.name, moduleReport.end.name);
DelegatingReporter.onModuleFinish(moduleReport);
}