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
|
#!/usr/bin/env node
const program = require('commander');
const { spawn } = require('child_process');
const JEST_ROUTE = 'spec/frontend';
const KARMA_ROUTE = 'spec/javascripts';
const COMMON_ARGS = ['--colors'];
program
.version('0.1.0')
.usage('[options] <file ...>')
.option('-p, --parallel', 'Run tests suites in parallel')
.parse(process.argv);
const runTests = paths => {
if (program.parallel) {
return Promise.all([runJest(paths), runKarma(paths)]);
} else {
return runJest(paths).then(() => runKarma(paths));
}
};
const spawnPromise = (cmd, args) => {
return new Promise((resolve, reject) => {
const proc = spawn('yarn', ['run', cmd, ...args]);
const output = data => `${cmd}: ${data}`;
proc.stdout.on('data', data => {
process.stdout.write(output(data));
});
proc.stderr.on('data', data => {
process.stderr.write(output(data));
});
proc.on('close', code => {
process.stdout.write(`${cmd} exited with code ${code}`);
if (code === 0) {
resolve();
} else {
reject();
}
});
});
};
const runJest = args => {
return spawnPromise('jest', [...COMMON_ARGS, ...toJestArgs(args)]);
};
const runKarma = args => {
return spawnPromise('karma', [...COMMON_ARGS, ...toKarmaArgs(args)]);
};
const replacePath = to => path =>
path
.replace(JEST_ROUTE, to)
.replace(KARMA_ROUTE, to)
.replace('app/assets/javascripts', to);
const toJestArgs = paths => paths.map(replacePath(JEST_ROUTE));
const toKarmaArgs = paths =>
paths.map(replacePath(KARMA_ROUTE)).reduce((acc, current) => acc.concat('-f', current), []);
const main = paths => {
runTests(paths)
.then(() => {
console.log('All tests passed!');
})
.catch(() => {
console.log('Some tests failed...');
});
};
main(program.args);
|