Skip to content

Commit

Permalink
Add an option to fail if no tests are found (#3672)
Browse files Browse the repository at this point in the history
* add tests for `--failWithNoTests`

* invert the logic
  • Loading branch information
EnoahNetzach authored and cpojer committed Oct 8, 2017
1 parent 88fec3d commit e04e451
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion integration_tests/__tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down
40 changes: 40 additions & 0 deletions integration_tests/__tests__/pass_with_no_tests-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
5 changes: 5 additions & 0 deletions integration_tests/pass_with_no_tests-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
6 changes: 6 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 11 additions & 2 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
notify: false,
onlyChanged: false,
outputFile: null,
passWithNoTests: false,
projects: [],
replname: null,
reporters: [],
Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export type GlobalConfig = {|
notify: boolean,
outputFile: ?Path,
onlyChanged: boolean,
passWithNoTests: boolean,
projects: Array<Glob>,
replname: ?string,
reporters: Array<ReporterConfig>,
Expand Down

0 comments on commit e04e451

Please sign in to comment.