Commit bd9774e4 authored by Paul Gascou-Vaillancourt's avatar Paul Gascou-Vaillancourt Committed by Mike Greiling

Add watch option to test script

parent ea1b24cb
......@@ -5,19 +5,24 @@ const { EOL } = require('os');
const program = require('commander');
const chalk = require('chalk');
const SUCCESS_CODE = 0;
const JEST_ROUTE = 'spec/frontend';
const KARMA_ROUTE = 'spec/javascripts';
const COMMON_ARGS = ['--colors'];
const JEST_ARGS = ['--passWithNoTests'];
const KARMA_ARGS = ['--no-fail-on-empty-test-suite'];
const SUCCESS_CODE = 0;
const jestArgs = [...COMMON_ARGS, '--passWithNoTests'];
const karmaArgs = [...COMMON_ARGS, '--no-fail-on-empty-test-suite'];
program
.version('0.1.0')
.usage('[options] <file ...>')
.option('-p, --parallel', 'Run tests suites in parallel')
.option(
'-w, --watch',
'Rerun tests when files change (tests will be run in parallel if this enabled)',
)
.parse(process.argv);
const shouldParallelize = program.parallel || program.watch;
const isSuccess = code => code === SUCCESS_CODE;
const combineExitCodes = codes => {
......@@ -31,7 +36,7 @@ const skipIfFail = fn => code => (isSuccess(code) ? fn() : code);
const endWithEOL = str => (str[str.length - 1] === '\n' ? str : `${str}${EOL}`);
const runTests = paths => {
if (program.parallel) {
if (shouldParallelize) {
return Promise.all([runJest(paths), runKarma(paths)]).then(combineExitCodes);
} else {
return runJest(paths).then(skipIfFail(() => runKarma(paths)));
......@@ -73,11 +78,11 @@ const spawnYarnScript = (cmd, args) => {
};
const runJest = args => {
return spawnYarnScript('jest', [...JEST_ARGS, ...COMMON_ARGS, ...toJestArgs(args)]);
return spawnYarnScript('jest', [...jestArgs, ...toJestArgs(args)]);
};
const runKarma = args => {
return spawnYarnScript('karma', [...KARMA_ARGS, ...COMMON_ARGS, ...toKarmaArgs(args)]);
return spawnYarnScript('karma', [...karmaArgs, ...toKarmaArgs(args)]);
};
const replacePath = to => path =>
......@@ -96,6 +101,10 @@ const toKarmaArgs = paths =>
paths.reduce((acc, path) => acc.concat('-f', replacePathForKarma(path)), []);
const main = paths => {
if (program.watch) {
jestArgs.push('--watch');
karmaArgs.push('--single-run', 'false', '--auto-watch');
}
runTests(paths).then(code => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
if (isSuccess(code)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment