-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
95 lines (76 loc) · 2.51 KB
/
index.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
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { TestResult } from './models';
const TEST_REGEX = 'spec\\.ts';
const COMPLETION_LABEL = 'Tests executed in';
function readDirRecursively(
dir: string,
paths = new Set<string>(),
): Set<string> {
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (new RegExp(TEST_REGEX, 'ig').test(file)) {
paths.add(filePath);
}
if (stat.isDirectory()) {
readDirRecursively(filePath, paths);
}
});
return paths;
}
function main(args: string[]): void {
console.time(COMPLETION_LABEL);
const defaultRoot = './';
const rootDirectory = args[2] || defaultRoot;
const testFilePaths = readDirRecursively(rootDirectory);
const testResults: Map<string, Set<TestResult>> = new Map();
const testRuns: any[] = [];
Array.from(testFilePaths.values()).forEach(path => {
const childProcess = child_process.fork(path);
testRuns.push(childProcess);
childProcess.on('message', (testResult: TestResult) => {
let results = new Set([testResult]);
if (testResults.has(testResult.suiteName)) {
results = testResults.get(testResult.suiteName).add(testResult);
}
testResults.set(testResult.suiteName, results);
});
});
let hasBeenReported = false;
setInterval(
() => {
const isEverythingFinished =
!testRuns.filter(run => run.connected === true).length;
if (isEverythingFinished && !hasBeenReported) {
printStatus(testResults);
console.log('\n');
console.timeEnd(COMPLETION_LABEL);
console.log('\n');
hasBeenReported = true;
process.exit(0);
}
},
);
}
function printStatus(testResults: Map<string, Set<TestResult>>): void {
const testSuiteNames = Array.from(testResults.keys()).sort();
testSuiteNames.forEach(suiteName => {
const results = testResults.get(suiteName);
const total = results.size;
const failed = Array.from(results.values())
.filter(testResult => !testResult.runStatus)
.length;
const passing = total - failed;
console.log(`\n${suiteName} (${total}/${passing})`);
results.forEach(result => {
const runStatus = result.runStatus;
console.log(`-- ${result.testName}: ${runStatus}`);
if (!result.runStatus) {
console.error(` Reason: ${result.failureReason}`);
}
});
});
}
main(process.argv);