Commit 334cfe47 authored by Paul Slaughter's avatar Paul Slaughter

Update FE test script

- Don't fail when no tests match
- Print the cmd prefix for each line in it's data
- Update exit code handling
- Add colors to summary message
parent 6f74a035
#!/usr/bin/env node #!/usr/bin/env node
const program = require('commander');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const { EOL } = require('os');
const program = require('commander');
const chalk = require('chalk');
const JEST_ROUTE = 'spec/frontend'; const JEST_ROUTE = 'spec/frontend';
const KARMA_ROUTE = 'spec/javascripts'; const KARMA_ROUTE = 'spec/javascripts';
const COMMON_ARGS = ['--colors']; const COMMON_ARGS = ['--colors'];
const JEST_ARGS = ['--passWithNoTests'];
const KARMA_ARGS = ['--no-fail-on-empty-test-suite'];
const SUCCESS_CODE = 0;
program program
.version('0.1.0') .version('0.1.0')
...@@ -13,18 +18,40 @@ program ...@@ -13,18 +18,40 @@ program
.option('-p, --parallel', 'Run tests suites in parallel') .option('-p, --parallel', 'Run tests suites in parallel')
.parse(process.argv); .parse(process.argv);
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}`);
const runTests = paths => { const runTests = paths => {
if (program.parallel) { if (program.parallel) {
return Promise.all([runJest(paths), runKarma(paths)]); return Promise.all([runJest(paths), runKarma(paths)]).then(combineExitCodes);
} else { } else {
return runJest(paths).then(() => runKarma(paths)); return runJest(paths).then(skipIfFail(() => runKarma(paths)));
} }
}; };
const spawnPromise = (cmd, args) => { const spawnYarnScript = (cmd, args) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const proc = spawn('yarn', ['run', cmd, ...args]); const proc = spawn('yarn', ['run', cmd, ...args]);
const output = data => `${cmd}: ${data}`; 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);
};
proc.stdout.on('data', data => { proc.stdout.on('data', data => {
process.stdout.write(output(data)); process.stdout.write(output(data));
...@@ -35,22 +62,22 @@ const spawnPromise = (cmd, args) => { ...@@ -35,22 +62,22 @@ const spawnPromise = (cmd, args) => {
}); });
proc.on('close', code => { proc.on('close', code => {
process.stdout.write(`${cmd} exited with code ${code}`); process.stdout.write(output(`exited with code ${code}`));
if (code === 0) {
resolve(); // We resolve even on a failure code because a `reject` would cause
} else { // Promise.all to reject immediately (without waiting for other promises)
reject(); // to finish.
} resolve(code);
}); });
}); });
}; };
const runJest = args => { const runJest = args => {
return spawnPromise('jest', [...COMMON_ARGS, ...toJestArgs(args)]); return spawnYarnScript('jest', [...JEST_ARGS, ...COMMON_ARGS, ...toJestArgs(args)]);
}; };
const runKarma = args => { const runKarma = args => {
return spawnPromise('karma', [...COMMON_ARGS, ...toKarmaArgs(args)]); return spawnYarnScript('karma', [...KARMA_ARGS, ...COMMON_ARGS, ...toKarmaArgs(args)]);
}; };
const replacePath = to => path => const replacePath = to => path =>
...@@ -59,18 +86,28 @@ const replacePath = to => path => ...@@ -59,18 +86,28 @@ const replacePath = to => path =>
.replace(KARMA_ROUTE, to) .replace(KARMA_ROUTE, to)
.replace('app/assets/javascripts', to); .replace('app/assets/javascripts', to);
const toJestArgs = paths => paths.map(replacePath(JEST_ROUTE)); const replacePathForJest = replacePath(JEST_ROUTE);
const replacePathForKarma = replacePath(KARMA_ROUTE);
const toJestArgs = paths => paths.map(replacePathForJest);
const toKarmaArgs = paths => const toKarmaArgs = paths =>
paths.map(replacePath(KARMA_ROUTE)).reduce((acc, current) => acc.concat('-f', current), []); paths.reduce((acc, path) => acc.concat('-f', replacePathForKarma(path)), []);
const main = paths => { const main = paths => {
runTests(paths) runTests(paths).then(code => {
.then(() => { console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.log('All tests passed!'); if (isSuccess(code)) {
}) console.log(chalk.bgGreen(chalk.black('All tests passed :)')));
.catch(() => { } else {
console.log('Some tests failed...'); console.log(chalk.bgRed(chalk.white(`Some tests failed :(`)));
}
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
if (!isSuccess(code)) {
process.exit(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