-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
85 lines (69 loc) · 1.93 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import Mocha from 'mocha';
import sequence from 'promise-map-series';
import path from 'path';
import minimist from 'minimist';
export function handleAbsolute(moduleId, root) {
if (path.isAbsolute(moduleId)) {
return moduleId;
} else {
return path.join(root, moduleId);
}
}
export function normalizeOptionAliases(_options) {
// don't mutating our arguments, rather deep clone and mutate the clone.
const options = JSON.parse(JSON.stringify(_options))
delete options._;
if (options.g) {
options.grep = options.g;
delete options.g;
}
if (options.i) {
options.invert = options.i;
delete options.i;
}
return options;
}
export class Runner {
constructor(mocha, root) {
this.mocha = mocha;
this.root = root;
}
async importModuleFiles(files) {
await sequence(files, id => this.importModule(id));
}
async importModule(module) {
const moduleId = handleAbsolute(module, this.root);
this.mocha.suite.emit('pre-require', global, moduleId, this.mocha);
try {
await import(moduleId);
} catch (e) {
if (e !== null && typeof e === 'object' && e.name === 'SyntaxError') {
e.message = `\n file: '${moduleId}'\n ${e.message}`;
}
throw e;
}
this.mocha.suite.emit('require', null, moduleId, this.mocha);
this.mocha.suite.emit('post-require', global, moduleId, this.mocha);
}
async run() {
return new Promise(resolve => {
this.mocha.run(failures => resolve({ failures }));
});
}
}
export async function main(argv) {
const options = minimist(argv);
const files = options._;
const mocha = new Mocha(normalizeOptionAliases(options));
const root = process.cwd();
const runner = new Runner(mocha, root);;
try {
await runner.importModuleFiles(files);
let { failures } = await runner.run();
process.exit(failures > 0 ? 1 : 0);
} catch(e) {
console.error(e);
process.exit(1);
}
}
main(process.argv.slice(2));