Skip to content

Commit d0d7dab

Browse files
authored
fix: correct message when no tests found and findRelatedTests passed (#12487)
1 parent 4dc9db2 commit d0d7dab

9 files changed

+171
-45
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378))
3737
- `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397))
3838
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
39+
- `[jest-core]` [**BREAKING**] Exit with status `1` if no tests are found with `--findRelatedTests` flag ([#12487](https://github.com/facebook/jest/pull/12487))
3940
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))
4041
- `[jest-environment-jsdom]` Log JSDOM errors more cleanly ([#12386](https://github.com/facebook/jest/pull/12386))
4142
- `[@jest/expect-utils]` [**BREAKING**] Fix false positives when looking for `undefined` prop ([#8923](https://github.com/facebook/jest/pull/8923))

Diff for: e2e/__tests__/noTestsFound.test.ts

+40-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ describe('No tests are found', () => {
1717
'/non/existing/path/',
1818
]);
1919

20-
expect(stdout).toMatch('No tests found');
20+
expect(stdout).toContain('No tests found, exiting with code 1');
21+
expect(stdout).toContain(
22+
'Run with `--passWithNoTests` to exit with code 0',
23+
);
2124
expect(exitCode).toBe(1);
2225
});
2326

@@ -28,34 +31,65 @@ describe('No tests are found', () => {
2831
'--passWithNoTests',
2932
]);
3033

31-
expect(stdout).toMatch('No tests found');
34+
expect(stdout).toContain('No tests found, exiting with code 0');
35+
expect(stdout).not.toContain(
36+
'Run with `--passWithNoTests` to exit with code 0',
37+
);
3238
expect(exitCode).toBe(0);
3339
});
3440

3541
test("doesn't fail the test suite if using --lastCommit", () => {
3642
// Since there are no files in DIR no tests will be found
3743
const {exitCode, stdout} = runJest(DIR, ['--lastCommit']);
3844

39-
expect(stdout).toMatch('No tests found');
45+
expect(stdout).toContain(
46+
'No tests found related to files changed since last commit.',
47+
);
48+
expect(stdout).not.toContain(
49+
'Run with `--passWithNoTests` to exit with code 0',
50+
);
4051
expect(exitCode).toBe(0);
4152
});
4253

4354
test("doesn't fail the test suite if using --onlyChanged", () => {
4455
// Since there are no files in DIR no tests will be found
4556
const {exitCode, stdout} = runJest(DIR, ['--onlyChanged']);
4657

47-
expect(stdout).toMatch('No tests found');
58+
expect(stdout).toContain(
59+
'No tests found related to files changed since last commit.',
60+
);
61+
expect(stdout).not.toContain(
62+
'Run with `--passWithNoTests` to exit with code 0',
63+
);
4864
expect(exitCode).toBe(0);
4965
});
5066

51-
test("doesn't fail the test suite if using --findRelatedTests", () => {
67+
test('fails the test suite if using --findRelatedTests', () => {
5268
// Since there are no files in DIR no tests will be found
5369
const {exitCode, stdout} = runJest(DIR, [
5470
'--findRelatedTests',
5571
'/non/existing/path',
5672
]);
5773

58-
expect(stdout).toMatch('No tests found');
74+
expect(stdout).toContain('No tests found, exiting with code 1');
75+
expect(stdout).toContain(
76+
'Run with `--passWithNoTests` to exit with code 0',
77+
);
78+
expect(exitCode).toBe(1);
79+
});
80+
81+
test("doesn't fail the test suite if using --findRelatedTests and --passWithNoTests", () => {
82+
// Since there are no files in DIR no tests will be found
83+
const {exitCode, stdout} = runJest(DIR, [
84+
'--findRelatedTests',
85+
'/non/existing/path',
86+
'--passWithNoTests',
87+
]);
88+
89+
expect(stdout).toContain('No tests found, exiting with code 0');
90+
expect(stdout).not.toContain(
91+
'Run with `--passWithNoTests` to exit with code 0',
92+
);
5993
expect(exitCode).toBe(0);
6094
});
6195
});

Diff for: packages/jest-core/src/__tests__/__snapshots__/getNoTestsFoundMessage.test.js.snap

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getNoTestsFoundMessage returns correct message when monitoring only changed 1`] = `
4+
Object {
5+
"exitWith0": true,
6+
"message": "<bold>No tests found related to files changed since last commit.</><dim></>
7+
<dim>Run Jest without \`-o\` or with \`--all\` to run all tests.</>",
8+
}
9+
`;
10+
11+
exports[`getNoTestsFoundMessage returns correct message when monitoring only failures 1`] = `
12+
Object {
13+
"exitWith0": false,
14+
"message": "<bold>No failed test found.</><dim></>
15+
<dim>Run Jest without \`--onlyFailures\` or with \`--all\` to run all tests.</>",
16+
}
17+
`;
18+
19+
exports[`getNoTestsFoundMessage returns correct message with findRelatedTests 1`] = `
20+
Object {
21+
"exitWith0": false,
22+
"message": "<bold>No tests found, exiting with code 1</>
23+
Run with \`--passWithNoTests\` to exit with code 0
24+
In <bold>/root/dir</>
25+
0 files checked across 0 projects. Run with \`--verbose\` for more details.
26+
Pattern: <yellow>/path/pattern</> - 0 matches",
27+
}
28+
`;
29+
30+
exports[`getNoTestsFoundMessage returns correct message with findRelatedTests 2`] = `
31+
Object {
32+
"exitWith0": true,
33+
"message": "<bold>No tests found, exiting with code 0</>",
34+
}
35+
`;
36+
37+
exports[`getNoTestsFoundMessage returns correct message with passWithNoTests 1`] = `
38+
Object {
39+
"exitWith0": true,
40+
"message": "<bold>No tests found, exiting with code 0</>",
41+
}
42+
`;
43+
44+
exports[`getNoTestsFoundMessage returns correct message with verbose option 1`] = `
45+
Object {
46+
"exitWith0": false,
47+
"message": "<bold>No tests found, exiting with code 1</>
48+
Run with \`--passWithNoTests\` to exit with code 0
49+
50+
Pattern: <yellow>/path/pattern</> - 0 matches",
51+
}
52+
`;
53+
54+
exports[`getNoTestsFoundMessage returns correct message without options 1`] = `
55+
Object {
56+
"exitWith0": false,
57+
"message": "<bold>No tests found, exiting with code 1</>
58+
Run with \`--passWithNoTests\` to exit with code 0
59+
In <bold>/root/dir</>
60+
0 files checked across 0 projects. Run with \`--verbose\` for more details.
61+
Pattern: <yellow>/path/pattern</> - 0 matches",
62+
}
63+
`;

Diff for: packages/jest-core/src/__tests__/getNoTestsFoundMessage.test.js renamed to packages/jest-core/src/__tests__/getNoTestsFoundMessage.test.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {makeGlobalConfig} from '@jest/test-utils';
9+
import type {Config} from '@jest/types';
810
import getNoTestsFoundMessage from '../getNoTestsFoundMessage';
911

12+
jest.mock('jest-util', () => ({
13+
...jest.requireActual('jest-util'),
14+
isInteractive: true,
15+
}));
16+
1017
describe('getNoTestsFoundMessage', () => {
11-
function createGlobalConfig(options) {
12-
return {
18+
function createGlobalConfig(options?: Partial<Config.GlobalConfig>) {
19+
return makeGlobalConfig({
1320
rootDir: '/root/dir',
1421
testPathPattern: '/path/pattern',
1522
...options,
16-
};
23+
});
1724
}
1825

1926
test('returns correct message when monitoring only failures', () => {
@@ -40,4 +47,12 @@ describe('getNoTestsFoundMessage', () => {
4047
const config = createGlobalConfig({passWithNoTests: true});
4148
expect(getNoTestsFoundMessage([], config)).toMatchSnapshot();
4249
});
50+
51+
test('returns correct message with findRelatedTests', () => {
52+
const config = createGlobalConfig({findRelatedTests: true});
53+
expect(getNoTestsFoundMessage([], config)).toMatchSnapshot();
54+
expect(
55+
getNoTestsFoundMessage([], {...config, passWithNoTests: true}),
56+
).toMatchSnapshot();
57+
});
4358
});

Diff for: packages/jest-core/src/getNoTestFound.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {TestRunData} from './types';
1313
export default function getNoTestFound(
1414
testRunData: TestRunData,
1515
globalConfig: Config.GlobalConfig,
16+
willExitWith0: boolean,
1617
): string {
1718
const testFiles = testRunData.reduce(
1819
(current, testRun) => current + (testRun.matches.total || 0),
@@ -30,6 +31,22 @@ export default function getNoTestFound(
3031
)} - 0 matches`;
3132
}
3233

34+
if (willExitWith0) {
35+
return (
36+
chalk.bold('No tests found, exiting with code 0') +
37+
'\n' +
38+
`In ${chalk.bold(globalConfig.rootDir)}` +
39+
'\n' +
40+
` ${pluralize('file', testFiles, 's')} checked across ${pluralize(
41+
'project',
42+
testRunData.length,
43+
's',
44+
)}. Run with \`--verbose\` for more details.` +
45+
'\n' +
46+
dataMessage
47+
);
48+
}
49+
3350
return (
3451
chalk.bold('No tests found, exiting with code 1') +
3552
'\n' +

Diff for: packages/jest-core/src/getNoTestFoundVerbose.ts

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {Stats, TestRunData} from './types';
1313
export default function getNoTestFoundVerbose(
1414
testRunData: TestRunData,
1515
globalConfig: Config.GlobalConfig,
16+
willExitWith0: boolean,
1617
): string {
1718
const individualResults = testRunData.map(testRun => {
1819
const stats = testRun.matches.stats || ({} as Stats);
@@ -57,6 +58,16 @@ export default function getNoTestFoundVerbose(
5758
)} - 0 matches`;
5859
}
5960

61+
if (willExitWith0) {
62+
return (
63+
chalk.bold('No tests found, exiting with code 0') +
64+
'\n' +
65+
individualResults.join('\n') +
66+
'\n' +
67+
dataMessage
68+
);
69+
}
70+
6071
return (
6172
chalk.bold('No tests found, exiting with code 1') +
6273
'\n' +

Diff for: packages/jest-core/src/getNoTestsFoundMessage.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,29 @@ import type {TestRunData} from './types';
1616
export default function getNoTestsFoundMessage(
1717
testRunData: TestRunData,
1818
globalConfig: Config.GlobalConfig,
19-
): string {
19+
): {exitWith0: boolean; message: string} {
20+
const exitWith0 =
21+
globalConfig.passWithNoTests ||
22+
globalConfig.lastCommit ||
23+
globalConfig.onlyChanged;
24+
2025
if (globalConfig.onlyFailures) {
21-
return getNoTestFoundFailed(globalConfig);
26+
return {exitWith0, message: getNoTestFoundFailed(globalConfig)};
2227
}
2328
if (globalConfig.onlyChanged) {
24-
return getNoTestFoundRelatedToChangedFiles(globalConfig);
29+
return {
30+
exitWith0,
31+
message: getNoTestFoundRelatedToChangedFiles(globalConfig),
32+
};
2533
}
2634
if (globalConfig.passWithNoTests) {
27-
return getNoTestFoundPassWithNoTests();
35+
return {exitWith0, message: getNoTestFoundPassWithNoTests()};
2836
}
29-
return testRunData.length === 1 || globalConfig.verbose
30-
? getNoTestFoundVerbose(testRunData, globalConfig)
31-
: getNoTestFound(testRunData, globalConfig);
37+
return {
38+
exitWith0,
39+
message:
40+
testRunData.length === 1 || globalConfig.verbose
41+
? getNoTestFoundVerbose(testRunData, globalConfig, exitWith0)
42+
: getNoTestFound(testRunData, globalConfig, exitWith0),
43+
};
3244
}

Diff for: packages/jest-core/src/runJest.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,12 @@ export default async function runJest({
220220
const hasTests = allTests.length > 0;
221221

222222
if (!hasTests) {
223-
const noTestsFoundMessage = getNoTestsFoundMessage(
223+
const {exitWith0, message: noTestsFoundMessage} = getNoTestsFoundMessage(
224224
testRunData,
225225
globalConfig,
226226
);
227227

228-
if (
229-
globalConfig.passWithNoTests ||
230-
globalConfig.findRelatedTests ||
231-
globalConfig.lastCommit ||
232-
globalConfig.onlyChanged
233-
) {
228+
if (exitWith0) {
234229
new CustomConsole(outputStream, outputStream).log(noTestsFoundMessage);
235230
} else {
236231
new CustomConsole(outputStream, outputStream).error(noTestsFoundMessage);

0 commit comments

Comments
 (0)