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

fix: do not run global hooks if there are no tests #7745

Merged
merged 3 commits into from
Jan 29, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `[jest-cli]` Break dependency cycle when using Jest programmatically ([#7707](https://github.com/facebook/jest/pull/7707))
- `[jest-config]` Extract setupFilesAfterEnv from preset ([#7724](https://github.com/facebook/jest/pull/7724))
- `[jest-cli]` Do not execute any `globalSetup` or `globalTeardown` if there are no tests to execute ([#7745](https://github.com/facebook/jest/pull/7745))

### Chore & Maintenance

Expand Down
19 changes: 19 additions & 0 deletions e2e/__tests__/globalSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@ test('should not call a globalSetup of a project if there are no tests to run fr
expect(fs.existsSync(project1DIR)).toBe(true);
expect(fs.existsSync(project2DIR)).toBe(false);
});

test('should not call any globalSetup if there are no tests to run', () => {
const configPath = path.resolve(
__dirname,
'../global-setup/projects.jest.config.js',
);

const result = runWithJson('global-setup', [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runWithJson actually fails without the fix, but I added the assertions either way to assert they don't run, even if we don't crash anymore

image

`--config=${configPath}`,
// onlyChanged ensures there are no tests to run
'--onlyChanged',
]);

expect(result.status).toBe(0);

expect(fs.existsSync(DIR)).toBe(false);
expect(fs.existsSync(project1DIR)).toBe(false);
expect(fs.existsSync(project2DIR)).toBe(false);
});
16 changes: 9 additions & 7 deletions packages/jest-cli/src/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ export default (async function runJest({
globalConfig = failedTestsCache.updateConfig(globalConfig);
}

if (!allTests.length) {
const hasTests = allTests.length > 0;

if (!hasTests) {
const noTestsFoundMessage = getNoTestsFoundMessage(
testRunData,
globalConfig,
Expand Down Expand Up @@ -250,7 +252,9 @@ export default (async function runJest({
collectHandles = collectNodeHandles();
}

await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'});
if (hasTests) {
await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'});
}

const results = await new TestScheduler(
globalConfig,
Expand All @@ -262,11 +266,9 @@ export default (async function runJest({

sequencer.cacheResults(allTests, results);

await runGlobalHook({
allTests,
globalConfig,
moduleName: 'globalTeardown',
});
if (hasTests) {
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'});
}

return processResults(results, {
collectHandles,
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import chalk from 'chalk';
import getChangedFilesPromise from './getChangedFilesPromise';
import exit from 'exit';
import HasteMap from 'jest-haste-map';
import {formatExecError} from 'jest-message-util';
import isValidPath from './lib/is_valid_path';
import {isInteractive, specialChars} from 'jest-util';
import {print as preRunMessagePrint} from './preRunMessage';
Expand Down Expand Up @@ -280,7 +281,10 @@ export default function watch(
// continuous watch mode execution. We need to reprint them to the
// terminal and give just a little bit of extra space so they fit below
// `preRunMessagePrint` message nicely.
console.error('\n\n' + chalk.red(error)),
console.error(
'\n\n' +
formatExecError(error, contexts[0].config, {noStackTrace: false}),
),
);
};

Expand Down