forked from cpojer/best-test-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
63 lines (59 loc) · 1.79 KB
/
index.mjs
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
import JestHasteMap from 'jest-haste-map';
import {cpus} from 'os';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import fs from 'fs';
import {Worker} from 'jest-worker';
import {join, relative} from 'path';
import chalk from 'chalk';
// Get the root path to our project (Like `__dirname`).
const root = dirname(fileURLToPath(import.meta.url));
const hasteMapOptions = {
extensions: ['js'],
maxWorkers: cpus().length,
name: 'best-test-framework',
platforms: [],
rootDir: root,
roots: [root],
};
const hasteMap = new JestHasteMap.default(hasteMapOptions);
await hasteMap.setupCachePath(hasteMapOptions);
const {hasteFS} = await hasteMap.build();
const testFiles = hasteFS.matchFilesWithGlob([
process.argv[2] ? `**/${process.argv[2]}*` : '**/*.test.js',
]);
const worker = new Worker(join(root, 'worker.js'), {
enableWorkerThreads: true,
});
let hasFailed = false;
await Promise.all(
Array.from(testFiles).map(async (testFile) => {
const {success, testResults, errorMessage} = await worker.runTest(testFile);
const status = success
? chalk.green.inverse.bold(' PASS ')
: chalk.red.inverse.bold(' FAIL ');
console.log(status + ' ' + chalk.dim(relative(root, testFile)));
if (!success) {
hasFailed = true;
if (testResults) {
testResults
.filter((result) => result.errors.length)
.forEach((result) =>
console.log(
result.testPath.slice(1).join(' ') + '\n' + result.errors[0],
),
);
} else if (errorMessage) {
console.log(' ' + errorMessage);
}
}
}),
);
worker.end();
if (hasFailed) {
console.log(
'\n' + chalk.red.bold('Test run failed, please fix all the failing tests.'),
);
// Set an exit code to indicate failure.
process.exitCode = 1;
}