Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly inject extraGlobals into the environment #10758

Merged
merged 2 commits into from Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
- `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744))
- `[jest-runtime, babel-jest]` Pass more ESM options to `@jest/transform` ([#10752](https://github.com/facebook/jest/pull/10752))
- `[jest-runtime]` Properly inject `extraGlobals` into the runtime ([#10758](https://github.com/facebook/jest/pull/10758))
- `[jest-transform]` Link to ESM docs on syntax errors ([#10748](https://github.com/facebook/jest/pull/10748))

### Chore & Maintenance
Expand Down
34 changes: 34 additions & 0 deletions e2e/__tests__/extraGlobals.test.ts
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {tmpdir} from 'os';
import * as path from 'path';
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
import {json as runJest} from '../runJest';

const DIR = path.resolve(tmpdir(), 'extra-globals');

beforeEach(() => {
cleanup(DIR);
createEmptyPackage(DIR, {jest: {extraGlobals: ['Math']}});
});

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

test('works with injected globals', () => {
writeFiles(DIR, {
'test.js': `
test('Math works when injected', () => {
expect(Math.floor(1.5)).toBe(1);
});
`,
});

const {exitCode} = runJest(DIR);

expect(exitCode).toBe(0);
});
5 changes: 3 additions & 2 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -1095,9 +1095,9 @@ class Runtime {

this.jestObjectCaches.set(filename, jestObject);

const lastArgs: [Jest | undefined, ...Array<any>] = [
const lastArgs: [Jest | undefined, ...Array<Global.Global>] = [
this._config.injectGlobals ? jestObject : undefined, // jest object
this._config.extraGlobals.map<unknown>(globalVariable => {
...this._config.extraGlobals.map<Global.Global>(globalVariable => {
if (this._environment.global[globalVariable]) {
return this._environment.global[globalVariable];
}
Expand Down Expand Up @@ -1126,6 +1126,7 @@ class Runtime {
module.path, // __dirname
module.filename, // __filename
this._environment.global, // global object
// @ts-expect-error
...lastArgs.filter(notEmpty),
);
} catch (error) {
Expand Down