-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
77 lines (66 loc) · 1.85 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const fs = require('fs');
const path = require('path');
const {PassThrough} = require('stream');
const tapMerge = require('tap-merge');
const merge = require('merge2');
const multistream = require('multistream');
const parallel = require('run-parallel');
const runNode = require('./lib/run-node');
const runChrome = require('./lib/run-chrome');
const reportCoverage = require('./lib/report-coverage');
async function run(opts, cb) {
if (!opts.node && !opts.browser) {
throw Error('No browser or node test entry specified');
}
const tests = [];
const coverageObjects = [];
const outputs = [];
function finish(code) {
reportCoverage(coverageObjects);
cb(code);
}
function runTest(runner, paths) {
if (typeof paths === 'string') {
paths = [paths];
}
if (!Array.isArray(paths)) {
throw new Error(
'invalid test - expected paths as string or string array but got ' +
paths
);
}
tests.push.apply(
tests,
paths.map(entry => async done => {
const out = PassThrough();
const err = PassThrough();
outputs.push(merge([out, err]));
const entryPath = path.resolve(process.cwd(), entry);
const sub = await runner(entryPath, (coverage, exitCode) => {
if (coverage) {
coverageObjects.push(coverage);
}
done(null, exitCode);
});
sub.stdout.pipe(out);
sub.stderr.pipe(err);
})
);
}
if (opts.node) {
runTest(runNode, opts.node);
}
if (opts.browser) {
runTest(runChrome, opts.browser);
}
parallel(tests, (err, results) => {
const finalCode = results.reduce((acc, code) => acc || code, 0);
finish(finalCode);
});
const allOutput = multistream(outputs);
const merged = tapMerge();
allOutput.pipe(merged);
return merged;
}
module.exports = run;