/
index.js
54 lines (47 loc) 路 1.35 KB
/
index.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
import { Piscina } from "piscina";
import supportsColor from "supports-color";
import { MessageChannel } from "worker_threads";
import os from "os";
/** @typedef {import("@jest/test-result").Test} Test */
const piscina = new Piscina({
filename: new URL("./worker-runner.js", import.meta.url).href,
maxThreads: os.cpus().length / 2,
env: {
// Workers don't have a tty; we whant them to inherit
// the color support level from the main thread.
FORCE_COLOR: supportsColor.stdout.level,
...process.env,
},
});
export default class LightRunner {
#config;
constructor(config) {
this.#config = config;
}
/**
* @param {Array<Test>} tests
* @param {*} watcher
* @param {*} onStart
* @param {*} onResult
* @param {*} onFailure
*/
runTests(tests, watcher, onStart, onResult, onFailure) {
const { updateSnapshot, testNamePattern } = this.#config;
return Promise.all(
tests.map(test => {
const mc = new MessageChannel();
mc.port2.onmessage = () => onStart(test);
mc.port2.unref();
return piscina
.run(
{ test, updateSnapshot, testNamePattern, port: mc.port1 },
{ transferList: [mc.port1] }
)
.then(
result => void onResult(test, result),
error => void onFailure(test, error)
);
})
);
}
}