From c799a0251d84a7e0c04d98e7d921a5a0e707c72f Mon Sep 17 00:00:00 2001 From: Emil Goldsmith Olesen Date: Fri, 12 Jan 2018 14:25:07 -0300 Subject: [PATCH] Make [jest-cli] not error when no tests are found with --findRelatedTests, --lastCommit or --onlyChanged options (#5127) --- CHANGELOG.md | 7 ++ .../__tests__/no_tests_found.test.js | 69 +++++++++++++++++++ .../__tests__/pass_with_no_tests.test.js | 39 ----------- .../package.json | 0 packages/jest-cli/src/run_jest.js | 7 +- 5 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 integration_tests/__tests__/no_tests_found.test.js delete mode 100644 integration_tests/__tests__/pass_with_no_tests.test.js rename integration_tests/{pass_with_no_tests-test => no_tests_found-test}/package.json (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 045c0580bc29..158332c07c47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ ## master + +### Features + +* `[jest-cli]` Make Jest exit without an error when no tests are found in + the case of `--lastCommit`, `--findRelatedTests`, or `--onlyChanged` options + having been passed to the CLI + ## jest 22.0.6 ### Fixes diff --git a/integration_tests/__tests__/no_tests_found.test.js b/integration_tests/__tests__/no_tests_found.test.js new file mode 100644 index 000000000000..5b24242a782b --- /dev/null +++ b/integration_tests/__tests__/no_tests_found.test.js @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +'use strict'; + +const path = require('path'); +const runJest = require('../runJest'); + +const DIR = path.resolve(__dirname, '../no_tests_found-test'); + +describe('No tests are found', () => { + test('fails the test suite in standard situation', () => { + const result = runJest(DIR, ['--testPathPattern', '/non/existing/path/']); + const status = result.status; + const stdout = result.stdout.toString(); + + expect(stdout).toMatch('No tests found'); + expect(status).toBe(1); + }); + + test("doesn't fail the test suite if --passWithNoTests passed", () => { + const result = runJest(DIR, [ + '--testPathPattern', + '/non/existing/path/', + '--passWithNoTests', + ]); + const status = result.status; + const stdout = result.stdout.toString(); + + expect(stdout).toMatch('No tests found'); + expect(status).toBe(0); + }); + + test("doesn't fail the test suite if using --lastCommit", () => { + // Since there are no files in DIR no tests will be found + const result = runJest(DIR, ['--lastCommit']); + const status = result.status; + const stdout = result.stdout.toString(); + + expect(stdout).toMatch('No tests found'); + expect(status).toBe(0); + }); + + test("doesn't fail the test suite if using --onlyChanged", () => { + // Since there are no files in DIR no tests will be found + const result = runJest(DIR, ['--onlyChanged']); + const status = result.status; + const stdout = result.stdout.toString(); + + expect(stdout).toMatch('No tests found'); + expect(status).toBe(0); + }); + + test("doesn't fail the test suite if using --findRelatedTests", () => { + // Since there are no files in DIR no tests will be found + const result = runJest(DIR, ['--findRelatedTests', '/non/existing/path']); + const status = result.status; + const stdout = result.stdout.toString(); + + expect(stdout).toMatch('No tests found'); + expect(status).toBe(0); + }); +}); diff --git a/integration_tests/__tests__/pass_with_no_tests.test.js b/integration_tests/__tests__/pass_with_no_tests.test.js deleted file mode 100644 index 02598f71e5c7..000000000000 --- a/integration_tests/__tests__/pass_with_no_tests.test.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -'use strict'; - -const path = require('path'); -const runJest = require('../runJest'); - -const DIR = path.resolve(__dirname, '../pass_with_no_tests-test'); - -describe('jest --passWithNoTests', () => { - test('fails the test suite if no files are found', () => { - const result = runJest(DIR, ['--testPathPattern', '/non/existing/path/']); - const status = result.status; - const stdout = result.stdout.toString(); - - expect(stdout).toMatch('No tests found'); - expect(status).toBe(1); - }); - - test("doesn't fail the test suite if no files are found", () => { - const result = runJest(DIR, [ - '--testPathPattern', - '/non/existing/path/', - '--passWithNoTests', - ]); - const status = result.status; - const stdout = result.stdout.toString(); - - expect(stdout).toMatch('No tests found'); - expect(status).toBe(0); - }); -}); diff --git a/integration_tests/pass_with_no_tests-test/package.json b/integration_tests/no_tests_found-test/package.json similarity index 100% rename from integration_tests/pass_with_no_tests-test/package.json rename to integration_tests/no_tests_found-test/package.json diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 29eb84cbfe0a..c2f975be5c9e 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -153,7 +153,12 @@ export default (async function runJest({ globalConfig, ); - if (globalConfig.passWithNoTests) { + if ( + globalConfig.passWithNoTests || + globalConfig.findRelatedTests || + globalConfig.lastCommit || + globalConfig.onlyChanged + ) { new Console(outputStream, outputStream).log(noTestsFoundMessage); } else { new Console(outputStream, outputStream).error(noTestsFoundMessage);