Skip to content

Commit e04e451

Browse files
EnoahNetzachcpojer
authored andcommitted
Add an option to fail if no tests are found (#3672)
* add tests for `--failWithNoTests` * invert the logic
1 parent 88fec3d commit e04e451

File tree

8 files changed

+66
-3
lines changed

8 files changed

+66
-3
lines changed

integration_tests/__tests__/config.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test('config as JSON', () => {
2020
]);
2121
const stdout = result.stdout.toString();
2222

23-
expect(result.status).toBe(0);
23+
expect(result.status).toBe(1);
2424
expect(stdout).toMatch('No tests found');
2525
});
2626

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
'use strict';
12+
13+
const path = require('path');
14+
const runJest = require('../runJest');
15+
16+
const DIR = path.resolve(__dirname, '../pass_with_no_tests-test');
17+
18+
describe('jest --passWithNoTests', () => {
19+
test('fails the test suite if no files are found', () => {
20+
const result = runJest(DIR, ['--testPathPattern', '/non/existing/path/']);
21+
const status = result.status;
22+
const stdout = result.stdout.toString();
23+
24+
expect(stdout).toMatch('No tests found');
25+
expect(status).toBe(1);
26+
});
27+
28+
test("doesn't fail the test suite if no files are found", () => {
29+
const result = runJest(DIR, [
30+
'--testPathPattern',
31+
'/non/existing/path/',
32+
'--passWithNoTests',
33+
]);
34+
const status = result.status;
35+
const stdout = result.stdout.toString();
36+
37+
expect(stdout).toMatch('No tests found');
38+
expect(status).toBe(0);
39+
});
40+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

packages/jest-cli/src/cli/args.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ export const options = {
340340
'also specified.',
341341
type: 'string',
342342
},
343+
passWithNoTests: {
344+
default: false,
345+
description:
346+
'Will not fail if no tests are found (for example while using `--testPathPattern`.)',
347+
type: 'boolean',
348+
},
343349
preset: {
344350
description: "A preset that is used as a base for Jest's configuration.",
345351
type: 'string',

packages/jest-cli/src/run_jest.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,18 @@ export default async function runJest({
124124
}
125125

126126
if (!allTests.length) {
127-
new Console(outputStream, outputStream).log(
128-
getNoTestsFoundMessage(testRunData, globalConfig),
127+
const noTestsFoundMessage = getNoTestsFoundMessage(
128+
testRunData,
129+
globalConfig,
129130
);
131+
132+
if (globalConfig.passWithNoTests) {
133+
new Console(outputStream, outputStream).log(noTestsFoundMessage);
134+
} else {
135+
new Console(outputStream, outputStream).error(noTestsFoundMessage);
136+
137+
process.exit(1);
138+
}
130139
} else if (
131140
allTests.length === 1 &&
132141
globalConfig.silent !== true &&

packages/jest-config/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const getConfigs = (
9696
notify: options.notify,
9797
onlyChanged: options.onlyChanged,
9898
outputFile: options.outputFile,
99+
passWithNoTests: options.passWithNoTests,
99100
projects: options.projects,
100101
replname: options.replname,
101102
reporters: options.reporters,

test_utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
3535
notify: false,
3636
onlyChanged: false,
3737
outputFile: null,
38+
passWithNoTests: false,
3839
projects: [],
3940
replname: null,
4041
reporters: [],

types/Config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export type GlobalConfig = {|
165165
notify: boolean,
166166
outputFile: ?Path,
167167
onlyChanged: boolean,
168+
passWithNoTests: boolean,
168169
projects: Array<Glob>,
169170
replname: ?string,
170171
reporters: Array<ReporterConfig>,

0 commit comments

Comments
 (0)