2019-04-10 15:55:11 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const { spawn } = require('child_process');
|
2019-05-10 08:17:50 -04:00
|
|
|
const { EOL } = require('os');
|
|
|
|
const program = require('commander');
|
|
|
|
const chalk = require('chalk');
|
2019-04-10 15:55:11 -04:00
|
|
|
|
2019-06-19 12:05:46 -04:00
|
|
|
const SUCCESS_CODE = 0;
|
2019-04-10 15:55:11 -04:00
|
|
|
const JEST_ROUTE = 'spec/frontend';
|
|
|
|
const KARMA_ROUTE = 'spec/javascripts';
|
|
|
|
const COMMON_ARGS = ['--colors'];
|
2019-06-19 12:05:46 -04:00
|
|
|
const jestArgs = [...COMMON_ARGS, '--passWithNoTests'];
|
|
|
|
const karmaArgs = [...COMMON_ARGS, '--no-fail-on-empty-test-suite'];
|
2019-04-10 15:55:11 -04:00
|
|
|
|
|
|
|
program
|
|
|
|
.usage('[options] <file ...>')
|
|
|
|
.option('-p, --parallel', 'Run tests suites in parallel')
|
2019-06-19 12:05:46 -04:00
|
|
|
.option(
|
|
|
|
'-w, --watch',
|
|
|
|
'Rerun tests when files change (tests will be run in parallel if this enabled)',
|
|
|
|
)
|
2019-04-10 15:55:11 -04:00
|
|
|
.parse(process.argv);
|
|
|
|
|
2019-06-19 12:05:46 -04:00
|
|
|
const shouldParallelize = program.parallel || program.watch;
|
|
|
|
|
2019-05-10 08:17:50 -04:00
|
|
|
const isSuccess = code => code === SUCCESS_CODE;
|
|
|
|
|
|
|
|
const combineExitCodes = codes => {
|
|
|
|
const firstFail = codes.find(x => !isSuccess(x));
|
|
|
|
|
|
|
|
return firstFail === undefined ? SUCCESS_CODE : firstFail;
|
|
|
|
};
|
|
|
|
|
|
|
|
const skipIfFail = fn => code => (isSuccess(code) ? fn() : code);
|
|
|
|
|
|
|
|
const endWithEOL = str => (str[str.length - 1] === '\n' ? str : `${str}${EOL}`);
|
|
|
|
|
2019-04-10 15:55:11 -04:00
|
|
|
const runTests = paths => {
|
2019-06-19 12:05:46 -04:00
|
|
|
if (shouldParallelize) {
|
2019-05-10 08:17:50 -04:00
|
|
|
return Promise.all([runJest(paths), runKarma(paths)]).then(combineExitCodes);
|
2019-04-10 15:55:11 -04:00
|
|
|
} else {
|
2019-05-10 08:17:50 -04:00
|
|
|
return runJest(paths).then(skipIfFail(() => runKarma(paths)));
|
2019-04-10 15:55:11 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-10 08:17:50 -04:00
|
|
|
const spawnYarnScript = (cmd, args) => {
|
2019-04-10 15:55:11 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const proc = spawn('yarn', ['run', cmd, ...args]);
|
2019-05-10 08:17:50 -04:00
|
|
|
const output = data => {
|
|
|
|
const text = data
|
|
|
|
.toString()
|
|
|
|
.split(/\r?\n/g)
|
|
|
|
.map((line, idx, { length }) =>
|
|
|
|
idx === length - 1 && !line ? line : `${chalk.gray(cmd)}: ${line}`,
|
|
|
|
)
|
|
|
|
.join(EOL);
|
|
|
|
|
|
|
|
return endWithEOL(text);
|
|
|
|
};
|
2019-04-10 15:55:11 -04:00
|
|
|
|
|
|
|
proc.stdout.on('data', data => {
|
|
|
|
process.stdout.write(output(data));
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.stderr.on('data', data => {
|
|
|
|
process.stderr.write(output(data));
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.on('close', code => {
|
2019-05-10 08:17:50 -04:00
|
|
|
process.stdout.write(output(`exited with code ${code}`));
|
|
|
|
|
|
|
|
// We resolve even on a failure code because a `reject` would cause
|
|
|
|
// Promise.all to reject immediately (without waiting for other promises)
|
|
|
|
// to finish.
|
|
|
|
resolve(code);
|
2019-04-10 15:55:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const runJest = args => {
|
2019-06-19 12:05:46 -04:00
|
|
|
return spawnYarnScript('jest', [...jestArgs, ...toJestArgs(args)]);
|
2019-04-10 15:55:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const runKarma = args => {
|
2019-06-19 12:05:46 -04:00
|
|
|
return spawnYarnScript('karma', [...karmaArgs, ...toKarmaArgs(args)]);
|
2019-04-10 15:55:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const replacePath = to => path =>
|
|
|
|
path
|
|
|
|
.replace(JEST_ROUTE, to)
|
|
|
|
.replace(KARMA_ROUTE, to)
|
|
|
|
.replace('app/assets/javascripts', to);
|
|
|
|
|
2019-05-10 08:17:50 -04:00
|
|
|
const replacePathForJest = replacePath(JEST_ROUTE);
|
|
|
|
|
|
|
|
const replacePathForKarma = replacePath(KARMA_ROUTE);
|
|
|
|
|
|
|
|
const toJestArgs = paths => paths.map(replacePathForJest);
|
2019-04-10 15:55:11 -04:00
|
|
|
|
|
|
|
const toKarmaArgs = paths =>
|
2019-05-10 08:17:50 -04:00
|
|
|
paths.reduce((acc, path) => acc.concat('-f', replacePathForKarma(path)), []);
|
2019-04-10 15:55:11 -04:00
|
|
|
|
|
|
|
const main = paths => {
|
2019-06-19 12:05:46 -04:00
|
|
|
if (program.watch) {
|
|
|
|
jestArgs.push('--watch');
|
|
|
|
karmaArgs.push('--single-run', 'false', '--auto-watch');
|
|
|
|
}
|
2019-05-10 08:17:50 -04:00
|
|
|
runTests(paths).then(code => {
|
|
|
|
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
|
|
|
|
if (isSuccess(code)) {
|
|
|
|
console.log(chalk.bgGreen(chalk.black('All tests passed :)')));
|
|
|
|
} else {
|
|
|
|
console.log(chalk.bgRed(chalk.white(`Some tests failed :(`)));
|
|
|
|
}
|
|
|
|
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
|
|
|
|
|
|
|
|
if (!isSuccess(code)) {
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
});
|
2019-04-10 15:55:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
main(program.args);
|