-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-runner.js
83 lines (68 loc) · 2.38 KB
/
test-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
import { fork } from 'node:child_process';
import fs from 'node:fs/promises';
import { events } from './../shared/vars.js';
class TestRunner {
constructor(folderName, formatter, reporter) {
this.folderName = folderName;
this.formatter = formatter;
this.reporter = reporter;
this.results = new Map();
this.tests = {
passing: 0,
failing: 0,
suites: 0,
};
this.startedAt = process.hrtime.bigint();
}
async runTests() {
const files = (await fs.readdir(this.folderName)).filter(item => /.test.js/.test(item));
const finished = []
for (const file of files) {
finished.push(this.runTestFile(file));
}
await Promise.all(finished)
this.reporter.printSummary(this.tests, this.formatter.calcElapsed(this.startedAt));
}
async runTestFile(file) {
const fullFileName = `${this.folderName}/${file}`;
const cp = fork(fullFileName);
const hooksNotPrint = ['before', 'beforeEach', 'after', 'afterEach'];
const eventsOutput = [
events.log,
events.testPass,
events.testFail
];
cp.on('message', ({ event, data }) => {
if (!event) return;
switch (event) {
case events.suiteStart:
if (data.tree) break;
this.tests.suites++;
break;
case events.testPass:
this.tests.passing++;
break;
case events.testFail:
this.tests.failing++;
break;
default:
break;
}
if (
hooksNotPrint.includes(data.name)
&&
event !== events.log
) return;
if (!eventsOutput.includes(event)) return;
const formattedResult = this.formatter.formatTestResult(data, event);
this.results.set(data.tree, this.results.get(data.tree) || []);
this.results.get(data.tree).push(formattedResult);
this.reporter.updateOutput(this.results, this.formatter);
});
cp.once('error', (err) => {
console.error(err)
})
return new Promise(resolve => cp.once('exit', resolve))
}
}
export default TestRunner;