Skip to content

Commit 2bbcc2c

Browse files
authored
fix: properly inject extraGlobals into the environment (#10758)
1 parent bc23f43 commit 2bbcc2c

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
1616
- `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744))
1717
- `[jest-runtime, babel-jest]` Pass more ESM options to `@jest/transform` ([#10752](https://github.com/facebook/jest/pull/10752))
18+
- `[jest-runtime]` Properly inject `extraGlobals` into the runtime ([#10758](https://github.com/facebook/jest/pull/10758))
1819
- `[jest-transform]` Link to ESM docs on syntax errors ([#10748](https://github.com/facebook/jest/pull/10748))
1920

2021
### Chore & Maintenance

e2e/__tests__/extraGlobals.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {tmpdir} from 'os';
9+
import * as path from 'path';
10+
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
11+
import {json as runJest} from '../runJest';
12+
13+
const DIR = path.resolve(tmpdir(), 'extra-globals');
14+
15+
beforeEach(() => {
16+
cleanup(DIR);
17+
createEmptyPackage(DIR, {jest: {extraGlobals: ['Math']}});
18+
});
19+
20+
afterAll(() => cleanup(DIR));
21+
22+
test('works with injected globals', () => {
23+
writeFiles(DIR, {
24+
'test.js': `
25+
test('Math works when injected', () => {
26+
expect(Math.floor(1.5)).toBe(1);
27+
});
28+
`,
29+
});
30+
31+
const {exitCode} = runJest(DIR);
32+
33+
expect(exitCode).toBe(0);
34+
});

packages/jest-runtime/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,9 @@ class Runtime {
10951095

10961096
this.jestObjectCaches.set(filename, jestObject);
10971097

1098-
const lastArgs: [Jest | undefined, ...Array<any>] = [
1098+
const lastArgs: [Jest | undefined, ...Array<Global.Global>] = [
10991099
this._config.injectGlobals ? jestObject : undefined, // jest object
1100-
this._config.extraGlobals.map<unknown>(globalVariable => {
1100+
...this._config.extraGlobals.map<Global.Global>(globalVariable => {
11011101
if (this._environment.global[globalVariable]) {
11021102
return this._environment.global[globalVariable];
11031103
}
@@ -1126,6 +1126,7 @@ class Runtime {
11261126
module.path, // __dirname
11271127
module.filename, // __filename
11281128
this._environment.global, // global object
1129+
// @ts-expect-error
11291130
...lastArgs.filter(notEmpty),
11301131
);
11311132
} catch (error) {

0 commit comments

Comments
 (0)