Create a unified script to run Jest & Karma tests

- Created scripts/frontend/test.js
- Updated test task to call Node script
This commit is contained in:
Paul Gascou-Vaillancourt 2019-04-10 15:55:11 -04:00 committed by Paul Slaughter
parent 26ee5c0599
commit 88b02af305
No known key found for this signature in database
GPG key ID: DF5690803C68282A
2 changed files with 78 additions and 1 deletions

View file

@ -19,7 +19,7 @@
"stylelint": "node node_modules/stylelint/bin/stylelint.js app/assets/stylesheets/**/*.* ee/app/assets/stylesheets/**/*.* !**/vendors/** --custom-formatter node_modules/stylelint-error-string-formatter", "stylelint": "node node_modules/stylelint/bin/stylelint.js app/assets/stylesheets/**/*.* ee/app/assets/stylesheets/**/*.* !**/vendors/** --custom-formatter node_modules/stylelint-error-string-formatter",
"stylelint-file": "node node_modules/stylelint/bin/stylelint.js", "stylelint-file": "node node_modules/stylelint/bin/stylelint.js",
"stylelint-create-utility-map": "node scripts/frontend/stylelint/stylelint-utility-map.js", "stylelint-create-utility-map": "node scripts/frontend/stylelint/stylelint-utility-map.js",
"test": "yarn jest && yarn karma", "test": "node scripts/frontend/test",
"webpack": "NODE_OPTIONS=\"--max-old-space-size=3584\" webpack --config config/webpack.config.js", "webpack": "NODE_OPTIONS=\"--max-old-space-size=3584\" webpack --config config/webpack.config.js",
"webpack-prod": "NODE_OPTIONS=\"--max-old-space-size=3584\" NODE_ENV=production webpack --config config/webpack.config.js" "webpack-prod": "NODE_OPTIONS=\"--max-old-space-size=3584\" NODE_ENV=production webpack --config config/webpack.config.js"
}, },

77
scripts/frontend/test.js Normal file
View file

@ -0,0 +1,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);