packages/cli: forward all args to jest and run in watch mode by default

This commit is contained in:
Patrik Oldsberg
2020-04-19 02:21:44 +02:00
parent e842489333
commit 15908eec6d
3 changed files with 41 additions and 11 deletions
+1
View File
@@ -65,6 +65,7 @@
"handlebars": "^4.7.3",
"html-webpack-plugin": "^3.2.0",
"inquirer": "^7.0.4",
"jest": "^25.1.0",
"ora": "^4.0.3",
"react": "^16.0.0",
"react-dev-utils": "^10.2.0",
+37 -8
View File
@@ -15,18 +15,47 @@
*/
import { Command } from 'commander';
import { run } from 'helpers/run';
import { paths } from 'helpers/paths';
import { runCheck } from 'helpers/run';
function includesAnyOf(hayStack: string[], ...needles: string[]) {
for (const needle of needles) {
if (hayStack.includes(needle)) {
return true;
}
}
return false;
}
export default async (cmd: Command) => {
const args = ['test', '--config', paths.resolveOwn('config/jest.js')];
// all args are forwarded to jest
const rawArgs = cmd.parent.rawArgs as string[];
const args = rawArgs.slice(rawArgs.indexOf('test') + 1);
if (cmd.watch) {
args.push('--watch');
}
if (cmd.coverage) {
args.push('--coverage');
// Only include our config if caller isn't passing their own config
if (!includesAnyOf(args, '-c', '--config')) {
args.push('--config', paths.resolveOwn('config/jest.js'));
}
await run('web-scripts', args, { stdio: 'inherit' });
// Run in watch mode unless in CI, coverage mode, or running all tests
if (
!process.env.CI &&
!args.includes('--coverage') &&
// explicitly no watching
!includesAnyOf(args, '--no-watch', '--watch=false', '--watchAll=false') &&
// already watching
!includesAnyOf(args, '--watch', '--watchAll')
) {
const isGitRepo = () => runCheck('git rev-parse --is-inside-work-tree');
const isMercurialRepo = () => runCheck('hg --cwd . root');
if ((await isGitRepo()) || (await isMercurialRepo())) {
args.push('--watch');
} else {
args.push('--watchAll');
}
}
// eslint-disable-next-line jest/no-jest-import
await require('jest').run(args);
};
+3 -3
View File
@@ -70,9 +70,9 @@ const main = (argv: string[]) => {
program
.command('test')
.option('--watch', 'Enable watch mode')
.option('--coverage', 'Report test coverage')
.description('Run all tests for package')
.allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args
.helpOption(', --backstage-cli-help') // Let Jest handle help
.description('Run tests, forwarding args to Jest, defaulting to watch mode')
.action(actionHandler(() => require('commands/testCommand')));
program