From e04e45102b3ebe486462d45db656f9b47f0e70e1 Mon Sep 17 00:00:00 2001 From: Fabrizio Castellarin Date: Sun, 8 Oct 2017 16:25:16 +0200 Subject: [PATCH] Add an option to fail if no tests are found (#3672) * add tests for `--failWithNoTests` * invert the logic --- integration_tests/__tests__/config.test.js | 2 +- .../__tests__/pass_with_no_tests-test.js | 40 +++++++++++++++++++ .../pass_with_no_tests-test/package.json | 5 +++ packages/jest-cli/src/cli/args.js | 6 +++ packages/jest-cli/src/run_jest.js | 13 +++++- packages/jest-config/src/index.js | 1 + test_utils.js | 1 + types/Config.js | 1 + 8 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 integration_tests/__tests__/pass_with_no_tests-test.js create mode 100644 integration_tests/pass_with_no_tests-test/package.json diff --git a/integration_tests/__tests__/config.test.js b/integration_tests/__tests__/config.test.js index 09a6c4a6ebd1..0a9921c78595 100644 --- a/integration_tests/__tests__/config.test.js +++ b/integration_tests/__tests__/config.test.js @@ -20,7 +20,7 @@ test('config as JSON', () => { ]); const stdout = result.stdout.toString(); - expect(result.status).toBe(0); + expect(result.status).toBe(1); expect(stdout).toMatch('No tests found'); }); diff --git a/integration_tests/__tests__/pass_with_no_tests-test.js b/integration_tests/__tests__/pass_with_no_tests-test.js new file mode 100644 index 000000000000..33b97195d1c8 --- /dev/null +++ b/integration_tests/__tests__/pass_with_no_tests-test.js @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @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/pass_with_no_tests-test/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/pass_with_no_tests-test/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index f045fce760fc..e60f377ce002 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -340,6 +340,12 @@ export const options = { 'also specified.', type: 'string', }, + passWithNoTests: { + default: false, + description: + 'Will not fail if no tests are found (for example while using `--testPathPattern`.)', + type: 'boolean', + }, preset: { description: "A preset that is used as a base for Jest's configuration.", type: 'string', diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index fc246b058296..5187fb31a2d8 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -124,9 +124,18 @@ export default async function runJest({ } if (!allTests.length) { - new Console(outputStream, outputStream).log( - getNoTestsFoundMessage(testRunData, globalConfig), + const noTestsFoundMessage = getNoTestsFoundMessage( + testRunData, + globalConfig, ); + + if (globalConfig.passWithNoTests) { + new Console(outputStream, outputStream).log(noTestsFoundMessage); + } else { + new Console(outputStream, outputStream).error(noTestsFoundMessage); + + process.exit(1); + } } else if ( allTests.length === 1 && globalConfig.silent !== true && diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 6e9fcf7105e4..5f75a03a233d 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -96,6 +96,7 @@ const getConfigs = ( notify: options.notify, onlyChanged: options.onlyChanged, outputFile: options.outputFile, + passWithNoTests: options.passWithNoTests, projects: options.projects, replname: options.replname, reporters: options.reporters, diff --git a/test_utils.js b/test_utils.js index 0f0c4f72ab61..53ad9e0acb99 100644 --- a/test_utils.js +++ b/test_utils.js @@ -35,6 +35,7 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = { notify: false, onlyChanged: false, outputFile: null, + passWithNoTests: false, projects: [], replname: null, reporters: [], diff --git a/types/Config.js b/types/Config.js index cb357ad502c3..3a453ba9e109 100644 --- a/types/Config.js +++ b/types/Config.js @@ -165,6 +165,7 @@ export type GlobalConfig = {| notify: boolean, outputFile: ?Path, onlyChanged: boolean, + passWithNoTests: boolean, projects: Array, replname: ?string, reporters: Array,