Skip to content

Commit

Permalink
fix: correct message when no tests found and findRelatedTests passed (
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Feb 24, 2022
1 parent 4dc9db2 commit d0d7dab
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- `[jest-config]` Correctly detect CI environment and update snapshots accordingly ([#12378](https://github.com/facebook/jest/pull/12378))
- `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397))
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
- `[jest-core]` [**BREAKING**] Exit with status `1` if no tests are found with `--findRelatedTests` flag ([#12487](https://github.com/facebook/jest/pull/12487))
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))
- `[jest-environment-jsdom]` Log JSDOM errors more cleanly ([#12386](https://github.com/facebook/jest/pull/12386))
- `[@jest/expect-utils]` [**BREAKING**] Fix false positives when looking for `undefined` prop ([#8923](https://github.com/facebook/jest/pull/8923))
Expand Down
46 changes: 40 additions & 6 deletions e2e/__tests__/noTestsFound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ describe('No tests are found', () => {
'/non/existing/path/',
]);

expect(stdout).toMatch('No tests found');
expect(stdout).toContain('No tests found, exiting with code 1');
expect(stdout).toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(1);
});

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

expect(stdout).toMatch('No tests found');
expect(stdout).toContain('No tests found, exiting with code 0');
expect(stdout).not.toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(0);
});

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

expect(stdout).toMatch('No tests found');
expect(stdout).toContain(
'No tests found related to files changed since last commit.',
);
expect(stdout).not.toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(0);
});

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

expect(stdout).toMatch('No tests found');
expect(stdout).toContain(
'No tests found related to files changed since last commit.',
);
expect(stdout).not.toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(0);
});

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

expect(stdout).toMatch('No tests found');
expect(stdout).toContain('No tests found, exiting with code 1');
expect(stdout).toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(1);
});

test("doesn't fail the test suite if using --findRelatedTests and --passWithNoTests", () => {
// Since there are no files in DIR no tests will be found
const {exitCode, stdout} = runJest(DIR, [
'--findRelatedTests',
'/non/existing/path',
'--passWithNoTests',
]);

expect(stdout).toContain('No tests found, exiting with code 0');
expect(stdout).not.toContain(
'Run with `--passWithNoTests` to exit with code 0',
);
expect(exitCode).toBe(0);
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getNoTestsFoundMessage returns correct message when monitoring only changed 1`] = `
Object {
"exitWith0": true,
"message": "<bold>No tests found related to files changed since last commit.</><dim></>
<dim>Run Jest without \`-o\` or with \`--all\` to run all tests.</>",
}
`;
exports[`getNoTestsFoundMessage returns correct message when monitoring only failures 1`] = `
Object {
"exitWith0": false,
"message": "<bold>No failed test found.</><dim></>
<dim>Run Jest without \`--onlyFailures\` or with \`--all\` to run all tests.</>",
}
`;
exports[`getNoTestsFoundMessage returns correct message with findRelatedTests 1`] = `
Object {
"exitWith0": false,
"message": "<bold>No tests found, exiting with code 1</>
Run with \`--passWithNoTests\` to exit with code 0
In <bold>/root/dir</>
0 files checked across 0 projects. Run with \`--verbose\` for more details.
Pattern: <yellow>/path/pattern</> - 0 matches",
}
`;
exports[`getNoTestsFoundMessage returns correct message with findRelatedTests 2`] = `
Object {
"exitWith0": true,
"message": "<bold>No tests found, exiting with code 0</>",
}
`;
exports[`getNoTestsFoundMessage returns correct message with passWithNoTests 1`] = `
Object {
"exitWith0": true,
"message": "<bold>No tests found, exiting with code 0</>",
}
`;
exports[`getNoTestsFoundMessage returns correct message with verbose option 1`] = `
Object {
"exitWith0": false,
"message": "<bold>No tests found, exiting with code 1</>
Run with \`--passWithNoTests\` to exit with code 0
Pattern: <yellow>/path/pattern</> - 0 matches",
}
`;
exports[`getNoTestsFoundMessage returns correct message without options 1`] = `
Object {
"exitWith0": false,
"message": "<bold>No tests found, exiting with code 1</>
Run with \`--passWithNoTests\` to exit with code 0
In <bold>/root/dir</>
0 files checked across 0 projects. Run with \`--verbose\` for more details.
Pattern: <yellow>/path/pattern</> - 0 matches",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
* LICENSE file in the root directory of this source tree.
*/

import {makeGlobalConfig} from '@jest/test-utils';
import type {Config} from '@jest/types';
import getNoTestsFoundMessage from '../getNoTestsFoundMessage';

jest.mock('jest-util', () => ({
...jest.requireActual('jest-util'),
isInteractive: true,
}));

describe('getNoTestsFoundMessage', () => {
function createGlobalConfig(options) {
return {
function createGlobalConfig(options?: Partial<Config.GlobalConfig>) {
return makeGlobalConfig({
rootDir: '/root/dir',
testPathPattern: '/path/pattern',
...options,
};
});
}

test('returns correct message when monitoring only failures', () => {
Expand All @@ -40,4 +47,12 @@ describe('getNoTestsFoundMessage', () => {
const config = createGlobalConfig({passWithNoTests: true});
expect(getNoTestsFoundMessage([], config)).toMatchSnapshot();
});

test('returns correct message with findRelatedTests', () => {
const config = createGlobalConfig({findRelatedTests: true});
expect(getNoTestsFoundMessage([], config)).toMatchSnapshot();
expect(
getNoTestsFoundMessage([], {...config, passWithNoTests: true}),
).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions packages/jest-core/src/getNoTestFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {TestRunData} from './types';
export default function getNoTestFound(
testRunData: TestRunData,
globalConfig: Config.GlobalConfig,
willExitWith0: boolean,
): string {
const testFiles = testRunData.reduce(
(current, testRun) => current + (testRun.matches.total || 0),
Expand All @@ -30,6 +31,22 @@ export default function getNoTestFound(
)} - 0 matches`;
}

if (willExitWith0) {
return (
chalk.bold('No tests found, exiting with code 0') +
'\n' +
`In ${chalk.bold(globalConfig.rootDir)}` +
'\n' +
` ${pluralize('file', testFiles, 's')} checked across ${pluralize(
'project',
testRunData.length,
's',
)}. Run with \`--verbose\` for more details.` +
'\n' +
dataMessage
);
}

return (
chalk.bold('No tests found, exiting with code 1') +
'\n' +
Expand Down
11 changes: 11 additions & 0 deletions packages/jest-core/src/getNoTestFoundVerbose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {Stats, TestRunData} from './types';
export default function getNoTestFoundVerbose(
testRunData: TestRunData,
globalConfig: Config.GlobalConfig,
willExitWith0: boolean,
): string {
const individualResults = testRunData.map(testRun => {
const stats = testRun.matches.stats || ({} as Stats);
Expand Down Expand Up @@ -57,6 +58,16 @@ export default function getNoTestFoundVerbose(
)} - 0 matches`;
}

if (willExitWith0) {
return (
chalk.bold('No tests found, exiting with code 0') +
'\n' +
individualResults.join('\n') +
'\n' +
dataMessage
);
}

return (
chalk.bold('No tests found, exiting with code 1') +
'\n' +
Expand Down
26 changes: 19 additions & 7 deletions packages/jest-core/src/getNoTestsFoundMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@ import type {TestRunData} from './types';
export default function getNoTestsFoundMessage(
testRunData: TestRunData,
globalConfig: Config.GlobalConfig,
): string {
): {exitWith0: boolean; message: string} {
const exitWith0 =
globalConfig.passWithNoTests ||
globalConfig.lastCommit ||
globalConfig.onlyChanged;

if (globalConfig.onlyFailures) {
return getNoTestFoundFailed(globalConfig);
return {exitWith0, message: getNoTestFoundFailed(globalConfig)};
}
if (globalConfig.onlyChanged) {
return getNoTestFoundRelatedToChangedFiles(globalConfig);
return {
exitWith0,
message: getNoTestFoundRelatedToChangedFiles(globalConfig),
};
}
if (globalConfig.passWithNoTests) {
return getNoTestFoundPassWithNoTests();
return {exitWith0, message: getNoTestFoundPassWithNoTests()};
}
return testRunData.length === 1 || globalConfig.verbose
? getNoTestFoundVerbose(testRunData, globalConfig)
: getNoTestFound(testRunData, globalConfig);
return {
exitWith0,
message:
testRunData.length === 1 || globalConfig.verbose
? getNoTestFoundVerbose(testRunData, globalConfig, exitWith0)
: getNoTestFound(testRunData, globalConfig, exitWith0),
};
}
9 changes: 2 additions & 7 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,12 @@ export default async function runJest({
const hasTests = allTests.length > 0;

if (!hasTests) {
const noTestsFoundMessage = getNoTestsFoundMessage(
const {exitWith0, message: noTestsFoundMessage} = getNoTestsFoundMessage(
testRunData,
globalConfig,
);

if (
globalConfig.passWithNoTests ||
globalConfig.findRelatedTests ||
globalConfig.lastCommit ||
globalConfig.onlyChanged
) {
if (exitWith0) {
new CustomConsole(outputStream, outputStream).log(noTestsFoundMessage);
} else {
new CustomConsole(outputStream, outputStream).error(noTestsFoundMessage);
Expand Down

0 comments on commit d0d7dab

Please sign in to comment.