Skip to content

Commit

Permalink
fix: do not run global hooks if there are no tests (jestjs#7745)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and captain-yossarian committed Jul 18, 2019
1 parent 7c08b87 commit 8af0057
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
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', [
`--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

0 comments on commit 8af0057

Please sign in to comment.