-
Notifications
You must be signed in to change notification settings - Fork 5
/
mocha-parallel.js
81 lines (68 loc) · 2.06 KB
/
mocha-parallel.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
// https://gist.github.com/msyea/c402de0c01e2de837153b2fa5e751f9f#file-mocha-parallel-js
const { promisify } = require("util");
const { spawn } = require("child_process");
const originalGlob = require("glob");
const { loadOptions } = require("mocha/lib/cli/options");
const unparse = require("yargs-unparser");
const { aliases } = require("mocha/lib/cli/run-option-metadata");
const mochaPath = require.resolve("mocha/bin/_mocha");
const glob = promisify(originalGlob);
const getMochaArgs = (path) => {
if (!Array.isArray(path)) {
path = [path];
}
const mochaArgs = loadOptions([...path, "--timeout", "30000"]);
return [].concat(mochaPath, unparse(mochaArgs, { alias: aliases }));
};
const children = [];
const runTests = (testFile) =>
new Promise((resolve, reject) => {
const child = spawn(process.execPath, getMochaArgs(testFile), {
env: {
NODE_ENV: "test",
},
});
children.push(child);
child.stdout.on("data", (data) => {
const line = data.toString().trim();
if (line) {
console.log(`${testFile}: ${line}`);
}
});
child.stderr.on("data", (data) => {
const line = data.toString().trim();
if (line) {
console.error(`${testFile}: ${line}`);
}
});
child.on("exit", (code, signal) => {
const index = children.indexOf(child);
if (index !== -1) {
children.splice(index, 1);
}
if (code || signal) {
reject(new Error(`something bad happend ${code || signal}`));
} else {
resolve();
}
});
});
glob("test/tests/**/*.js")
.then((testFiles) => {
return Promise.all(testFiles.map(runTests));
})
.then(() => {
console.log("all tests pass");
process.exit(0);
})
.catch((err) => {
console.error("some tests failed", err);
process.exit(1);
});
// terminate children.
process.on("SIGINT", () => {
children.forEach((child) => {
child.kill("SIGINT"); // calls runner.abort()
child.kill("SIGTERM"); // if that didn't work, we're probably in an infinite loop, so make it die.
});
});