Skip to content

Commit 1cb30a1

Browse files
authored
Rename "moduleLoader" to "runtime" and "extraGlobals" to "sandboxInjectedGlobals" configuration options (#10817)
1 parent 97466b4 commit 1cb30a1

File tree

19 files changed

+177
-63
lines changed

19 files changed

+177
-63
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354))
99
- `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355))
1010
- `[jest-config, @jest/types]` Add `ci` to `GlobalConfig` ([#12378](https://github.com/facebook/jest/pull/12378))
11+
- `[jest-config]` [**BREAKING**] Rename `moduleLoader` to `runtime` ([#10817](https://github.com/facebook/jest/pull/10817))
12+
- `[jest-config]` [**BREAKING**] Rename `extraGlobals` to `sandboxInjectedGlobals` ([#10817](https://github.com/facebook/jest/pull/10817))
1113
- `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440))
1214
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
1315
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))

docs/Configuration.md

+29-17
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,6 @@ Jest will run `.mjs` and `.js` files with nearest `package.json`'s `type` field
375375
}
376376
```
377377

378-
### `extraGlobals` \[array<string>]
379-
380-
Default: `undefined`
381-
382-
Test files run inside a [vm](https://nodejs.org/api/vm.html), which slows calls to global context properties (e.g. `Math`). With this option you can specify extra properties to be defined inside the vm for faster lookups.
383-
384-
For example, if your tests call `Math` often, you can pass it by setting `extraGlobals`.
385-
386-
```json
387-
{
388-
...
389-
"jest": {
390-
"extraGlobals": ["Math"]
391-
}
392-
}
393-
```
394-
395378
### `forceCoverageMatch` \[array<string>]
396379

397380
Default: `['']`
@@ -907,6 +890,35 @@ async function runTests(
907890

908891
If you need to restrict your test-runner to only run in serial rather than being executed in parallel your class should have the property `isSerial` to be set as `true`.
909892

893+
### `sandboxInjectedGlobals` \[array<string>]
894+
895+
:::tip
896+
897+
Renamed from `extraGlobals` in Jest 28.
898+
899+
:::
900+
901+
Default: `undefined`
902+
903+
Test files run inside a [vm](https://nodejs.org/api/vm.html), which slows calls to global context properties (e.g. `Math`). With this option you can specify extra properties to be defined inside the vm for faster lookups.
904+
905+
For example, if your tests call `Math` often, you can pass it by setting `sandboxInjectedGlobals`.
906+
907+
```json
908+
{
909+
...
910+
"jest": {
911+
"sandboxInjectedGlobals": ["Math"]
912+
}
913+
}
914+
```
915+
916+
:::note
917+
918+
This option has no effect if you use [native ESM](ECMAScriptModules.md).
919+
920+
:::
921+
910922
### `setupFiles` \[array]
911923

912924
Default: `[]`

e2e/__tests__/__snapshots__/showConfig.test.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
1616
"detectOpenHandles": false,
1717
"errorOnDeprecated": false,
1818
"extensionsToTreatAsEsm": [],
19-
"extraGlobals": [],
2019
"forceCoverageMatch": [],
2120
"globals": {},
2221
"haste": {
@@ -49,6 +48,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
4948
"<<REPLACED_ROOT_DIR>>"
5049
],
5150
"runner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-runner/build/index.js",
51+
"sandboxInjectedGlobals": [],
5252
"setupFiles": [],
5353
"setupFilesAfterEnv": [],
5454
"skipFilter": false,

e2e/__tests__/extraGlobals.test.ts renamed to e2e/__tests__/sandboxInjectedGlobals.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const DIR = path.resolve(tmpdir(), 'extra-globals');
1414

1515
beforeEach(() => {
1616
cleanup(DIR);
17-
createEmptyPackage(DIR, {jest: {extraGlobals: ['Math']}});
17+
createEmptyPackage(DIR, {jest: {sandboxInjectedGlobals: ['Math']}});
1818
});
1919

2020
afterAll(() => cleanup(DIR));

packages/jest-config/src/Deprecated.ts

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ const deprecatedOptions: DeprecatedOptions = {
1717
'"browser"',
1818
)} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as shown in the documentation: https://jestjs.io/docs/configuration#resolver-string`,
1919

20+
extraGlobals: (_options: {extraGlobals?: string}) => ` Option ${chalk.bold(
21+
'"extraGlobals"',
22+
)} was replaced by ${chalk.bold('"sandboxInjectedGlobals"')}.
23+
24+
Please update your configuration.`,
25+
26+
moduleLoader: (_options: {moduleLoader?: string}) => ` Option ${chalk.bold(
27+
'"moduleLoader"',
28+
)} was replaced by ${chalk.bold('"runtime"')}.
29+
30+
Please update your configuration.`,
31+
2032
preprocessorIgnorePatterns: (options: {
2133
preprocessorIgnorePatterns?: Array<string>;
2234
}) => ` Option ${chalk.bold(

packages/jest-config/src/ValidConfig.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const initialOptions: Config.InitialOptions = {
4949
errorOnDeprecated: false,
5050
expand: false,
5151
extensionsToTreatAsEsm: [],
52-
extraGlobals: [],
5352
filter: '<rootDir>/filter.js',
5453
forceCoverageMatch: ['**/*.t.js'],
5554
forceExit: false,
@@ -76,7 +75,6 @@ const initialOptions: Config.InitialOptions = {
7675
maxWorkers: '50%',
7776
moduleDirectories: ['node_modules'],
7877
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
79-
moduleLoader: '<rootDir>',
8078
moduleNameMapper: {
8179
'^React$': '<rootDir>/node_modules/react',
8280
},
@@ -105,6 +103,8 @@ const initialOptions: Config.InitialOptions = {
105103
roots: ['<rootDir>'],
106104
runTestsByPath: false,
107105
runner: 'jest-runner',
106+
runtime: '<rootDir>',
107+
sandboxInjectedGlobals: [],
108108
setupFiles: ['<rootDir>/setup.js'],
109109
setupFilesAfterEnv: ['<rootDir>/testSetupFile.js'],
110110
silent: true,

packages/jest-config/src/__tests__/__snapshots__/normalize.test.ts.snap

+48
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,54 @@ exports[`extensionsToTreatAsEsm throws on .mjs 1`] = `
132132
<red></>"
133133
`;
134134

135+
exports[`extraGlobals logs a deprecation warning when \`extraGlobals\` is used 1`] = `
136+
[MockFunction] {
137+
"calls": Array [
138+
Array [
139+
"<yellow><bold><bold>●</><bold> Deprecation Warning</>:</>
140+
<yellow></>
141+
<yellow> Option <bold>"extraGlobals"</> was replaced by <bold>"sandboxInjectedGlobals"</>.</>
142+
<yellow></>
143+
<yellow> Please update your configuration.</>
144+
<yellow></>
145+
<yellow> <bold>Configuration Documentation:</></>
146+
<yellow> https://jestjs.io/docs/configuration</>
147+
<yellow></>",
148+
],
149+
],
150+
"results": Array [
151+
Object {
152+
"type": "return",
153+
"value": undefined,
154+
},
155+
],
156+
}
157+
`;
158+
159+
exports[`moduleLoader logs a deprecation warning when \`moduleLoader\` is used 1`] = `
160+
[MockFunction] {
161+
"calls": Array [
162+
Array [
163+
"<yellow><bold><bold>●</><bold> Deprecation Warning</>:</>
164+
<yellow></>
165+
<yellow> Option <bold>"moduleLoader"</> was replaced by <bold>"runtime"</>.</>
166+
<yellow></>
167+
<yellow> Please update your configuration.</>
168+
<yellow></>
169+
<yellow> <bold>Configuration Documentation:</></>
170+
<yellow> https://jestjs.io/docs/configuration</>
171+
<yellow></>",
172+
],
173+
],
174+
"results": Array [
175+
Object {
176+
"type": "return",
177+
"value": undefined,
178+
},
179+
],
180+
}
181+
`;
182+
135183
exports[`preset throws when module was found but no "jest-preset.js" or "jest-preset.json" files 1`] = `
136184
"<red><bold><bold>● </><bold>Validation Error</>:</>
137185
<red></>

packages/jest-config/src/__tests__/normalize.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1936,3 +1936,39 @@ describe('testURL', () => {
19361936
expect(console.warn).toMatchSnapshot();
19371937
});
19381938
});
1939+
1940+
describe('extraGlobals', () => {
1941+
beforeEach(() => {
1942+
jest.mocked(console.warn).mockImplementation(() => {});
1943+
});
1944+
1945+
it('logs a deprecation warning when `extraGlobals` is used', async () => {
1946+
await normalize(
1947+
{
1948+
extraGlobals: ['Math'],
1949+
rootDir: '/root/',
1950+
},
1951+
{} as Config.Argv,
1952+
);
1953+
1954+
expect(console.warn).toMatchSnapshot();
1955+
});
1956+
});
1957+
1958+
describe('moduleLoader', () => {
1959+
beforeEach(() => {
1960+
jest.mocked(console.warn).mockImplementation(() => {});
1961+
});
1962+
1963+
it('logs a deprecation warning when `moduleLoader` is used', async () => {
1964+
await normalize(
1965+
{
1966+
moduleLoader: '<rootDir>/runtime.js',
1967+
rootDir: '/root/',
1968+
},
1969+
{} as Config.Argv,
1970+
);
1971+
1972+
expect(console.warn).toMatchSnapshot();
1973+
});
1974+
});

packages/jest-config/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ const groupOptions = (
184184
displayName: options.displayName,
185185
errorOnDeprecated: options.errorOnDeprecated,
186186
extensionsToTreatAsEsm: options.extensionsToTreatAsEsm,
187-
extraGlobals: options.extraGlobals,
188187
filter: options.filter,
189188
forceCoverageMatch: options.forceCoverageMatch,
190189
globalSetup: options.globalSetup,
@@ -194,7 +193,6 @@ const groupOptions = (
194193
injectGlobals: options.injectGlobals,
195194
moduleDirectories: options.moduleDirectories,
196195
moduleFileExtensions: options.moduleFileExtensions,
197-
moduleLoader: options.moduleLoader,
198196
moduleNameMapper: options.moduleNameMapper,
199197
modulePathIgnorePatterns: options.modulePathIgnorePatterns,
200198
modulePaths: options.modulePaths,
@@ -207,6 +205,8 @@ const groupOptions = (
207205
rootDir: options.rootDir,
208206
roots: options.roots,
209207
runner: options.runner,
208+
runtime: options.runtime,
209+
sandboxInjectedGlobals: options.sandboxInjectedGlobals,
210210
setupFiles: options.setupFiles,
211211
setupFilesAfterEnv: options.setupFilesAfterEnv,
212212
skipFilter: options.skipFilter,

packages/jest-config/src/normalize.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ export default async function normalize(
734734
case 'dependencyExtractor':
735735
case 'globalSetup':
736736
case 'globalTeardown':
737-
case 'moduleLoader':
737+
case 'runtime':
738738
case 'snapshotResolver':
739739
case 'testResultsProcessor':
740740
case 'testRunner':
@@ -979,7 +979,6 @@ export default async function normalize(
979979
case 'errorOnDeprecated':
980980
case 'expand':
981981
case 'extensionsToTreatAsEsm':
982-
case 'extraGlobals':
983982
case 'globals':
984983
case 'findRelatedTests':
985984
case 'forceCoverageMatch':
@@ -1004,6 +1003,7 @@ export default async function normalize(
10041003
case 'restoreMocks':
10051004
case 'rootDir':
10061005
case 'runTestsByPath':
1006+
case 'sandboxInjectedGlobals':
10071007
case 'silent':
10081008
case 'skipFilter':
10091009
case 'skipNodeResolution':
@@ -1209,8 +1209,8 @@ export default async function normalize(
12091209
newOptions.projects = [];
12101210
}
12111211

1212-
if (!newOptions.extraGlobals) {
1213-
newOptions.extraGlobals = [];
1212+
if (!newOptions.sandboxInjectedGlobals) {
1213+
newOptions.sandboxInjectedGlobals = [];
12141214
}
12151215

12161216
if (!newOptions.forceExit) {

packages/jest-core/src/lib/__tests__/__snapshots__/logDebugMessages.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ exports[`prints the config object 1`] = `
1313
"detectOpenHandles": false,
1414
"errorOnDeprecated": false,
1515
"extensionsToTreatAsEsm": [],
16-
"extraGlobals": [],
1716
"forceCoverageMatch": [],
1817
"globals": {},
1918
"haste": {},
@@ -22,7 +21,6 @@ exports[`prints the config object 1`] = `
2221
"moduleFileExtensions": [
2322
"js"
2423
],
25-
"moduleLoader": "/test_module_loader_path",
2624
"moduleNameMapper": [],
2725
"modulePathIgnorePatterns": [],
2826
"modulePaths": [],
@@ -36,6 +34,8 @@ exports[`prints the config object 1`] = `
3634
"path/to/dir/test"
3735
],
3836
"runner": "jest-runner",
37+
"runtime": "/test_module_loader_path",
38+
"sandboxInjectedGlobals": [],
3939
"setupFiles": [],
4040
"setupFilesAfterEnv": [],
4141
"skipFilter": false,

packages/jest-environment/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type ModuleWrapper = (
2525
__dirname: string,
2626
__filename: Module['filename'],
2727
jest?: Jest,
28-
...extraGlobals: Array<Global.Global[keyof Global.Global]>
28+
...sandboxInjectedGlobals: Array<Global.Global[keyof Global.Global]>
2929
) => unknown;
3030

3131
export interface JestEnvironmentConfig {

packages/jest-runner/src/runTest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ async function runTestInternal(
115115
: projectConfig.testRunner,
116116
);
117117
const Runtime: typeof RuntimeClass = interopRequireDefault(
118-
projectConfig.moduleLoader
119-
? require(projectConfig.moduleLoader)
118+
projectConfig.runtime
119+
? require(projectConfig.runtime)
120120
: require('jest-runtime'),
121121
).default;
122122

packages/jest-runtime/src/__tests__/runtime_wrap.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ describe('Runtime', () => {
2323
});
2424

2525
it('injects "extra globals"', async () => {
26-
const runtime = await createRuntime(__filename, {extraGlobals: ['Math']});
26+
const runtime = await createRuntime(__filename, {
27+
sandboxInjectedGlobals: ['Math'],
28+
});
2729

2830
expect(
2931
runtime.wrapCodeInModuleWrapper('module.exports = "Hello!"'),

packages/jest-runtime/src/index.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1472,15 +1472,17 @@ export default class Runtime {
14721472

14731473
const lastArgs: [Jest | undefined, ...Array<Global.Global>] = [
14741474
this._config.injectGlobals ? jestObject : undefined, // jest object
1475-
...this._config.extraGlobals.map<Global.Global>(globalVariable => {
1476-
if (this._environment.global[globalVariable]) {
1477-
return this._environment.global[globalVariable];
1478-
}
1475+
...this._config.sandboxInjectedGlobals.map<Global.Global>(
1476+
globalVariable => {
1477+
if (this._environment.global[globalVariable]) {
1478+
return this._environment.global[globalVariable];
1479+
}
14791480

1480-
throw new Error(
1481-
`You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,
1482-
);
1483-
}),
1481+
throw new Error(
1482+
`You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,
1483+
);
1484+
},
1485+
),
14841486
];
14851487

14861488
if (!this._mainModule && filename === this._testPath) {
@@ -2221,7 +2223,7 @@ export default class Runtime {
22212223
'__dirname',
22222224
'__filename',
22232225
this._config.injectGlobals ? 'jest' : undefined,
2224-
...this._config.extraGlobals,
2226+
...this._config.sandboxInjectedGlobals,
22252227
].filter(notEmpty);
22262228
}
22272229

0 commit comments

Comments
 (0)