Skip to content

Commit 8e69441

Browse files
authored
fix: do not run global hooks if there are no tests (#7745)
1 parent 2e2d2c8 commit 8e69441

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

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

1011
### Chore & Maintenance
1112

e2e/__tests__/globalSetup.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,22 @@ test('should not call a globalSetup of a project if there are no tests to run fr
103103
expect(fs.existsSync(project1DIR)).toBe(true);
104104
expect(fs.existsSync(project2DIR)).toBe(false);
105105
});
106+
107+
test('should not call any globalSetup if there are no tests to run', () => {
108+
const configPath = path.resolve(
109+
__dirname,
110+
'../global-setup/projects.jest.config.js',
111+
);
112+
113+
const result = runWithJson('global-setup', [
114+
`--config=${configPath}`,
115+
// onlyChanged ensures there are no tests to run
116+
'--onlyChanged',
117+
]);
118+
119+
expect(result.status).toBe(0);
120+
121+
expect(fs.existsSync(DIR)).toBe(false);
122+
expect(fs.existsSync(project1DIR)).toBe(false);
123+
expect(fs.existsSync(project2DIR)).toBe(false);
124+
});

packages/jest-cli/src/runJest.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ export default (async function runJest({
217217
globalConfig = failedTestsCache.updateConfig(globalConfig);
218218
}
219219

220-
if (!allTests.length) {
220+
const hasTests = allTests.length > 0;
221+
222+
if (!hasTests) {
221223
const noTestsFoundMessage = getNoTestsFoundMessage(
222224
testRunData,
223225
globalConfig,
@@ -250,7 +252,9 @@ export default (async function runJest({
250252
collectHandles = collectNodeHandles();
251253
}
252254

253-
await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'});
255+
if (hasTests) {
256+
await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'});
257+
}
254258

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

263267
sequencer.cacheResults(allTests, results);
264268

265-
await runGlobalHook({
266-
allTests,
267-
globalConfig,
268-
moduleName: 'globalTeardown',
269-
});
269+
if (hasTests) {
270+
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'});
271+
}
270272

271273
return processResults(results, {
272274
collectHandles,

packages/jest-cli/src/watch.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import chalk from 'chalk';
1717
import getChangedFilesPromise from './getChangedFilesPromise';
1818
import exit from 'exit';
1919
import HasteMap from 'jest-haste-map';
20+
import {formatExecError} from 'jest-message-util';
2021
import isValidPath from './lib/is_valid_path';
2122
import {isInteractive, specialChars} from 'jest-util';
2223
import {print as preRunMessagePrint} from './preRunMessage';
@@ -280,7 +281,10 @@ export default function watch(
280281
// continuous watch mode execution. We need to reprint them to the
281282
// terminal and give just a little bit of extra space so they fit below
282283
// `preRunMessagePrint` message nicely.
283-
console.error('\n\n' + chalk.red(error)),
284+
console.error(
285+
'\n\n' +
286+
formatExecError(error, contexts[0].config, {noStackTrace: false}),
287+
),
284288
);
285289
};
286290

0 commit comments

Comments
 (0)