diff --git a/CHANGELOG.md b/CHANGELOG.md index a21417b9a07e..df7b9de0fdb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/e2e/__tests__/globalSetup.test.js b/e2e/__tests__/globalSetup.test.js index 44b409f6f0b4..55ccdad7c410 100644 --- a/e2e/__tests__/globalSetup.test.js +++ b/e2e/__tests__/globalSetup.test.js @@ -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); +}); diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index 121cb201c72e..7b4894e6ac0a 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -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, @@ -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, @@ -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, diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index 2ea55110d318..3a060b1474e8 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -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'; @@ -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}), + ), ); };