diff --git a/packages/cli/package.json b/packages/cli/package.json index 340b67f03a..9f968b4f17 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/packages/cli/src/commands/testCommand.ts b/packages/cli/src/commands/testCommand.ts index 10a4fb54f3..1a1b298b9c 100644 --- a/packages/cli/src/commands/testCommand.ts +++ b/packages/cli/src/commands/testCommand.ts @@ -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); }; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 3ad43faa76..cdca2fa1ed 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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