Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to fail if no tests are found #3672

Merged
merged 2 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/__tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 @@ 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 @@ -126,9 +126,18 @@ const runJest = async ({
}

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 @@ -97,6 +97,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 @@ -36,6 +36,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 @@ -164,6 +164,7 @@ export type GlobalConfig = {|
notify: boolean,
outputFile: ?Path,
onlyChanged: boolean,
passWithNoTests: boolean,
projects: Array<Glob>,
replname: ?string,
reporters: Array<ReporterConfig>,
Expand Down