-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestExecutor.ts
194 lines (169 loc) · 5.15 KB
/
TestExecutor.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
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
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
InstrumentationDataMap,
MetaDataMap,
} from "@syntest/instrumentation-javascript";
import cloneDeep = require("lodash.clonedeep");
import { Runner } from "mocha";
import Mocha = require("mocha");
import { JavaScriptExecutionStatus } from "../../search/JavaScriptExecutionResult";
import { AssertionData } from "./AssertionData";
import { SilentMochaReporter } from "./SilentMochaReporter";
export type Message = RunMessage | DoneMessage;
export type RunMessage = {
message: "run";
silent: boolean;
esm: boolean;
paths: string[];
timeout: number;
};
export type DoneMessage = {
message: "done";
suites: Suite[];
stats: Mocha.Stats;
instrumentationData: InstrumentationDataMap;
metaData: MetaDataMap;
assertionData?: AssertionData;
error?: string;
};
export type Suite = {
tests: Test[];
};
export type Test = {
status: JavaScriptExecutionStatus;
error?: Error | undefined;
duration: number;
};
process.on("uncaughtException", (reason) => {
throw reason;
});
process.on("unhandledRejection", (reason) => {
throw reason;
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on("message", async (data: Message) => {
if (typeof data !== "object") {
throw new TypeError("Invalid data received from child process");
}
if (data.message === "run") {
await runMocha(data.silent, data.esm, data.paths, data.timeout);
}
});
async function runMocha(
silent: boolean,
esm: boolean,
paths: string[],
timeout: number
) {
const argv: Mocha.MochaOptions = <Mocha.MochaOptions>(<unknown>{
reporter: silent ? SilentMochaReporter : undefined,
// diff: false,
// checkLeaks: false,
// slow: 75,
timeout: timeout,
// watch: false,
// parallel: false,
// recursive: false,
// sort: false,
});
const mocha = new Mocha(argv); // require('ts-node/register')
if (esm) {
// eslint-disable-next-line unicorn/prefer-module
require("regenerator-runtime/runtime");
// eslint-disable-next-line unicorn/prefer-module, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires
require("@babel/register")({
// eslint-disable-next-line unicorn/prefer-module
presets: [require.resolve("@babel/preset-env")],
});
}
for (const _path of paths) {
// eslint-disable-next-line unicorn/prefer-module
delete require.cache[_path];
mocha.addFile(_path);
}
let runner: Runner;
// Finally, run mocha.
await new Promise((resolve) => {
runner = mocha.run((failures) => resolve(failures));
});
const suites: Suite[] = runner.suite.suites.map((suite) => {
return {
tests: suite.tests.map((test) => {
let status: JavaScriptExecutionStatus;
if (test.isPassed()) {
status = JavaScriptExecutionStatus.PASSED;
} else if (test.timedOut) {
status = JavaScriptExecutionStatus.TIMED_OUT;
} else {
status = JavaScriptExecutionStatus.FAILED;
}
return {
status: status,
error:
test && test.err
? {
name: test.err.name,
message: test.err.message,
stack: test.err.stack,
}
: undefined,
duration: test.duration,
};
}),
};
});
// Retrieve execution traces
const result: DoneMessage = {
message: "done",
suites: suites,
stats: runner.stats,
instrumentationData: cloneDeep(
(<GlobalType>(<unknown>global)).__coverage__
),
metaData: cloneDeep((<GlobalType>(<unknown>global)).__meta__),
assertionData: cloneDeep((<GlobalType>(<unknown>global)).__assertion__),
};
resetInstrumentationData();
(<GlobalType>(<unknown>global)).__meta__ = undefined;
(<GlobalType>(<unknown>global)).__assertion__ = undefined;
process.send(result);
mocha.dispose();
}
function resetInstrumentationData() {
const coverage = (<GlobalType>(<unknown>global)).__coverage__;
if (coverage === undefined) {
return;
}
for (const key of Object.keys(coverage)) {
for (const statementKey of Object.keys(coverage[key].s)) {
coverage[key].s[statementKey] = 0;
}
for (const functionKey of Object.keys(coverage[key].f)) {
coverage[key].f[functionKey] = 0;
}
for (const branchKey of Object.keys(coverage[key].b)) {
coverage[key].b[branchKey] = [0, 0];
}
}
}
type GlobalType = {
__coverage__: InstrumentationDataMap;
__meta__: MetaDataMap;
__assertion__: AssertionData;
};