diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce5d755682a..470461bfe0e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - `[docs]` Warn about unexpected behavior / bug of node-notifier when using the `notify` options. - `[jest-resolver]` Use `resolve` package to implement custom module resolution ([#9520](https://github.com/facebook/jest/pull/9520)) +- `[jest-runtime]` Move execution of `setupFiles` to `jest-runner` ([#9596](https://github.com/facebook/jest/pull/9596)) - `[@jest/reporters]` Remove unused dependencies and type exports ([#9462](https://github.com/facebook/jest/pull/9462)) - `[website]` Update pictures of reports when matchers fail ([#9214](https://github.com/facebook/jest/pull/9214)) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 379515f2c500..f0b54eb259b0 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -155,9 +155,7 @@ async function jasmine2( testPath, }); - config.setupFilesAfterEnv.forEach((path: Config.Path) => - runtime.requireModule(path), - ); + config.setupFilesAfterEnv.forEach(path => runtime.requireModule(path)); if (globalConfig.enabledTestsMap) { env.specFilter = (spec: Spec) => { diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index ff9b4fdafedb..b34255ead229 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -163,6 +163,8 @@ async function runTestInternal( const start = Date.now(); + config.setupFiles.forEach(path => runtime!.requireModule(path)); + const sourcemapOptions: sourcemapSupport.Options = { environment: 'node', handleUncaughtExceptions: false, diff --git a/packages/jest-runtime/src/__mocks__/createRuntime.js b/packages/jest-runtime/src/__mocks__/createRuntime.js index 0a2932e87176..1efc081eabfa 100644 --- a/packages/jest-runtime/src/__mocks__/createRuntime.js +++ b/packages/jest-runtime/src/__mocks__/createRuntime.js @@ -7,7 +7,7 @@ import path from 'path'; -module.exports = function createRuntime(filename, config) { +module.exports = async function createRuntime(filename, config) { const NodeEnvironment = require('jest-environment-node'); const Runtime = require('../'); @@ -37,22 +37,26 @@ module.exports = function createRuntime(filename, config) { const environment = new NodeEnvironment(config); environment.global.console = console; - return Runtime.createHasteMap(config, {maxWorkers: 1, resetCache: false}) - .build() - .then(hasteMap => { - const runtime = new Runtime( - config, - environment, - Runtime.createResolver(config, hasteMap.moduleMap), - ); - - runtime.__mockRootPath = path.join(config.rootDir, 'root.js'); - runtime.__mockSubdirPath = path.join( - config.rootDir, - 'subdir2', - 'module_dir', - 'module_dir_module.js', - ); - return runtime; - }); + + const hasteMap = await Runtime.createHasteMap(config, { + maxWorkers: 1, + resetCache: false, + }).build(); + + const runtime = new Runtime( + config, + environment, + Runtime.createResolver(config, hasteMap.moduleMap), + ); + + config.setupFiles.forEach(path => runtime.requireModule(path)); + + runtime.__mockRootPath = path.join(config.rootDir, 'root.js'); + runtime.__mockSubdirPath = path.join( + config.rootDir, + 'subdir2', + 'module_dir', + 'module_dir_module.js', + ); + return runtime; }; diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 1da49004242a..26cfb8136467 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -93,6 +93,9 @@ export async function run( setGlobal(environment.global, 'jestGlobalConfig', globalConfig); const runtime = new Runtime(config, environment, hasteMap.resolver); + + config.setupFiles.forEach(path => runtime.requireModule(path)); + runtime.requireModule(filePath); } catch (e) { console.error(chalk.red(e.stack || e)); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 99fb087f4779..c96b2912e5e6 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -185,12 +185,6 @@ class Runtime { } this.resetModules(); - - if (config.setupFiles.length) { - for (let i = 0; i < config.setupFiles.length; i++) { - this.requireModule(config.setupFiles[i]); - } - } } static shouldInstrument = shouldInstrument; diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index db8872b0df36..e7b81c46931c 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -5,36 +5,20 @@ * LICENSE file in the root directory of this source tree. */ -import clearLine from './clearLine'; -import createDirectory from './createDirectory'; -import ErrorWithStack from './ErrorWithStack'; -import installCommonGlobals from './installCommonGlobals'; -import interopRequireDefault from './interopRequireDefault'; -import isInteractive from './isInteractive'; -import isPromise from './isPromise'; -import setGlobal from './setGlobal'; -import deepCyclicCopy from './deepCyclicCopy'; -import convertDescriptorToString from './convertDescriptorToString'; +export {default as clearLine} from './clearLine'; +export {default as createDirectory} from './createDirectory'; +export {default as ErrorWithStack} from './ErrorWithStack'; +export {default as installCommonGlobals} from './installCommonGlobals'; +export {default as interopRequireDefault} from './interopRequireDefault'; +export {default as isInteractive} from './isInteractive'; +export {default as isPromise} from './isPromise'; +export {default as setGlobal} from './setGlobal'; +export {default as deepCyclicCopy} from './deepCyclicCopy'; +export {default as convertDescriptorToString} from './convertDescriptorToString'; import * as specialChars from './specialChars'; -import replacePathSepForGlob from './replacePathSepForGlob'; -import testPathPatternToRegExp from './testPathPatternToRegExp'; +export {default as replacePathSepForGlob} from './replacePathSepForGlob'; +export {default as testPathPatternToRegExp} from './testPathPatternToRegExp'; import * as preRunMessage from './preRunMessage'; -import pluralize from './pluralize'; +export {default as pluralize} from './pluralize'; -export { - ErrorWithStack, - clearLine, - convertDescriptorToString, - createDirectory, - deepCyclicCopy, - installCommonGlobals, - interopRequireDefault, - isInteractive, - isPromise, - pluralize, - preRunMessage, - replacePathSepForGlob, - setGlobal, - specialChars, - testPathPatternToRegExp, -}; +export {preRunMessage, specialChars};