Skip to content

Commit 56565cc

Browse files
authored
chore: move execution of setupFiles to jest-runner (#9596)
1 parent 3d09c5c commit 56565cc

7 files changed

Lines changed: 44 additions & 58 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
- `[docs]` Warn about unexpected behavior / bug of node-notifier when using the `notify` options.
3636
- `[jest-resolver]` Use `resolve` package to implement custom module resolution ([#9520](https://github.com/facebook/jest/pull/9520))
37+
- `[jest-runtime]` Move execution of `setupFiles` to `jest-runner` ([#9596](https://github.com/facebook/jest/pull/9596))
3738
- `[@jest/reporters]` Remove unused dependencies and type exports ([#9462](https://github.com/facebook/jest/pull/9462))
3839
- `[website]` Update pictures of reports when matchers fail ([#9214](https://github.com/facebook/jest/pull/9214))
3940

packages/jest-jasmine2/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ async function jasmine2(
155155
testPath,
156156
});
157157

158-
config.setupFilesAfterEnv.forEach((path: Config.Path) =>
159-
runtime.requireModule(path),
160-
);
158+
config.setupFilesAfterEnv.forEach(path => runtime.requireModule(path));
161159

162160
if (globalConfig.enabledTestsMap) {
163161
env.specFilter = (spec: Spec) => {

packages/jest-runner/src/runTest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ async function runTestInternal(
163163

164164
const start = Date.now();
165165

166+
config.setupFiles.forEach(path => runtime!.requireModule(path));
167+
166168
const sourcemapOptions: sourcemapSupport.Options = {
167169
environment: 'node',
168170
handleUncaughtExceptions: false,

packages/jest-runtime/src/__mocks__/createRuntime.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import path from 'path';
99

10-
module.exports = function createRuntime(filename, config) {
10+
module.exports = async function createRuntime(filename, config) {
1111
const NodeEnvironment = require('jest-environment-node');
1212
const Runtime = require('../');
1313

@@ -37,22 +37,26 @@ module.exports = function createRuntime(filename, config) {
3737

3838
const environment = new NodeEnvironment(config);
3939
environment.global.console = console;
40-
return Runtime.createHasteMap(config, {maxWorkers: 1, resetCache: false})
41-
.build()
42-
.then(hasteMap => {
43-
const runtime = new Runtime(
44-
config,
45-
environment,
46-
Runtime.createResolver(config, hasteMap.moduleMap),
47-
);
48-
49-
runtime.__mockRootPath = path.join(config.rootDir, 'root.js');
50-
runtime.__mockSubdirPath = path.join(
51-
config.rootDir,
52-
'subdir2',
53-
'module_dir',
54-
'module_dir_module.js',
55-
);
56-
return runtime;
57-
});
40+
41+
const hasteMap = await Runtime.createHasteMap(config, {
42+
maxWorkers: 1,
43+
resetCache: false,
44+
}).build();
45+
46+
const runtime = new Runtime(
47+
config,
48+
environment,
49+
Runtime.createResolver(config, hasteMap.moduleMap),
50+
);
51+
52+
config.setupFiles.forEach(path => runtime.requireModule(path));
53+
54+
runtime.__mockRootPath = path.join(config.rootDir, 'root.js');
55+
runtime.__mockSubdirPath = path.join(
56+
config.rootDir,
57+
'subdir2',
58+
'module_dir',
59+
'module_dir_module.js',
60+
);
61+
return runtime;
5862
};

packages/jest-runtime/src/cli/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export async function run(
9393
setGlobal(environment.global, 'jestGlobalConfig', globalConfig);
9494

9595
const runtime = new Runtime(config, environment, hasteMap.resolver);
96+
97+
config.setupFiles.forEach(path => runtime.requireModule(path));
98+
9699
runtime.requireModule(filePath);
97100
} catch (e) {
98101
console.error(chalk.red(e.stack || e));

packages/jest-runtime/src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ class Runtime {
185185
}
186186

187187
this.resetModules();
188-
189-
if (config.setupFiles.length) {
190-
for (let i = 0; i < config.setupFiles.length; i++) {
191-
this.requireModule(config.setupFiles[i]);
192-
}
193-
}
194188
}
195189

196190
static shouldInstrument = shouldInstrument;

packages/jest-util/src/index.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,20 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import clearLine from './clearLine';
9-
import createDirectory from './createDirectory';
10-
import ErrorWithStack from './ErrorWithStack';
11-
import installCommonGlobals from './installCommonGlobals';
12-
import interopRequireDefault from './interopRequireDefault';
13-
import isInteractive from './isInteractive';
14-
import isPromise from './isPromise';
15-
import setGlobal from './setGlobal';
16-
import deepCyclicCopy from './deepCyclicCopy';
17-
import convertDescriptorToString from './convertDescriptorToString';
8+
export {default as clearLine} from './clearLine';
9+
export {default as createDirectory} from './createDirectory';
10+
export {default as ErrorWithStack} from './ErrorWithStack';
11+
export {default as installCommonGlobals} from './installCommonGlobals';
12+
export {default as interopRequireDefault} from './interopRequireDefault';
13+
export {default as isInteractive} from './isInteractive';
14+
export {default as isPromise} from './isPromise';
15+
export {default as setGlobal} from './setGlobal';
16+
export {default as deepCyclicCopy} from './deepCyclicCopy';
17+
export {default as convertDescriptorToString} from './convertDescriptorToString';
1818
import * as specialChars from './specialChars';
19-
import replacePathSepForGlob from './replacePathSepForGlob';
20-
import testPathPatternToRegExp from './testPathPatternToRegExp';
19+
export {default as replacePathSepForGlob} from './replacePathSepForGlob';
20+
export {default as testPathPatternToRegExp} from './testPathPatternToRegExp';
2121
import * as preRunMessage from './preRunMessage';
22-
import pluralize from './pluralize';
22+
export {default as pluralize} from './pluralize';
2323

24-
export {
25-
ErrorWithStack,
26-
clearLine,
27-
convertDescriptorToString,
28-
createDirectory,
29-
deepCyclicCopy,
30-
installCommonGlobals,
31-
interopRequireDefault,
32-
isInteractive,
33-
isPromise,
34-
pluralize,
35-
preRunMessage,
36-
replacePathSepForGlob,
37-
setGlobal,
38-
specialChars,
39-
testPathPatternToRegExp,
40-
};
24+
export {preRunMessage, specialChars};

0 commit comments

Comments
 (0)