From 477f46193c3026e3473b573ce0d445c32a0cfb2e Mon Sep 17 00:00:00 2001 From: Kevin Huynh Date: Sun, 7 Oct 2018 14:26:35 -0700 Subject: [PATCH] Fix unhandled error when a bad revision is provided to `changedSince` (#7115) --- CHANGELOG.md | 1 + .../jest_changed_files.test.js.snap | 21 +++++++++++ e2e/__tests__/jest_changed_files.test.js | 37 +++++++++++++++++++ .../jest-cli/src/getChangedFilesPromise.js | 14 +++++++ 4 files changed, 73 insertions(+) create mode 100644 e2e/__tests__/__snapshots__/jest_changed_files.test.js.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 4babad50435c..28fc48be6596 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - `[jest-jasmine2]` Fail synchronous test timeouts ([#7074](https://github.com/facebook/jest/pull/7074)) - `[jest-jasmine2]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) - `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) +- `[jest-cli]` Fix unhandled error when a bad revision is provided to `changedSince` ([#7115](https://github.com/facebook/jest/pull/7115)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/jest_changed_files.test.js.snap b/e2e/__tests__/__snapshots__/jest_changed_files.test.js.snap new file mode 100644 index 000000000000..61d5013154f9 --- /dev/null +++ b/e2e/__tests__/__snapshots__/jest_changed_files.test.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`handles a bad revision for "changedSince", for git 1`] = ` +" + + ● Test suite failed to run + + fatal: bad revision '^blablabla' + +" +`; + +exports[`handles a bad revision for "changedSince", for hg 1`] = ` +" + + ● Test suite failed to run + + abort: unknown revision 'blablabla'! + +" +`; diff --git a/e2e/__tests__/jest_changed_files.test.js b/e2e/__tests__/jest_changed_files.test.js index 4498dd14598b..d0aa0a02d35a 100644 --- a/e2e/__tests__/jest_changed_files.test.js +++ b/e2e/__tests__/jest_changed_files.test.js @@ -17,6 +17,7 @@ import { } from '../../packages/jest-changed-files/src'; import {skipSuiteOnWindows} from '../../scripts/ConditionalTest'; import {cleanup, run, writeFiles} from '../Utils'; +import runJest from '../runJest'; skipSuiteOnWindows(); @@ -243,6 +244,24 @@ test('monitors only root paths for git', async () => { ).toEqual(['file2.txt', 'file3.txt']); }); +test('handles a bad revision for "changedSince", for git', async () => { + writeFiles(DIR, { + '.watchmanconfig': '', + '__tests__/file1.test.js': `require('../file1'); test('file1', () => {});`, + 'file1.js': 'module.exports = {}', + 'package.json': '{}', + }); + + run(`${GIT} init`, DIR); + run(`${GIT} add .`, DIR); + run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); + + const {status, stderr} = runJest(DIR, ['--changedSince=blablabla']); + + expect(status).toBe(1); + expect(stderr).toMatchSnapshot(); +}); + test('gets changed files for hg', async () => { if (process.env.CI) { // Circle and Travis have very old version of hg (v2, and current @@ -371,3 +390,21 @@ test('monitors only root paths for hg', async () => { .sort(), ).toEqual(['file2.txt', 'file3.txt']); }); + +test('handles a bad revision for "changedSince", for hg', async () => { + writeFiles(DIR, { + '.watchmanconfig': '', + '__tests__/file1.test.js': `require('../file1'); test('file1', () => {});`, + 'file1.js': 'module.exports = {}', + 'package.json': '{}', + }); + + run(`${HG} init`, DIR); + run(`${HG} add .`, DIR); + run(`${HG} commit -m "first"`, DIR); + + const {status, stderr} = runJest(DIR, ['--changedSince=blablabla']); + + expect(status).toBe(1); + expect(stderr).toMatchSnapshot(); +}); diff --git a/packages/jest-cli/src/getChangedFilesPromise.js b/packages/jest-cli/src/getChangedFilesPromise.js index d8bc8ed59e84..7dc849f1a8c1 100644 --- a/packages/jest-cli/src/getChangedFilesPromise.js +++ b/packages/jest-cli/src/getChangedFilesPromise.js @@ -10,6 +10,8 @@ import type {GlobalConfig, ProjectConfig} from 'types/Config'; import type {ChangedFilesPromise} from 'types/ChangedFiles'; import {getChangedFilesForRoots} from 'jest-changed-files'; +import {formatExecError} from 'jest-message-util'; +import chalk from 'chalk'; export default ( globalConfig: GlobalConfig, @@ -24,6 +26,18 @@ export default ( changedSince: globalConfig.changedSince, lastCommit: globalConfig.lastCommit, withAncestor: globalConfig.changedFilesWithAncestor, + }).catch(e => { + const message = formatExecError(e, configs[0], {noStackTrace: true}) + .split('\n') + .filter(line => !line || !line.includes('Command failed:')) + .join('\n'); + + console.error(chalk.red(`\n\n${message}`)); + + process.exit(1); + + // We do process.exit, so this is dead code + return Promise.reject(e); }); }