Skip to content

Commit

Permalink
fix: properly inject extraGlobals into the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 31, 2020
1 parent 95169d3 commit 395953c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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 * as path from 'path';
import {tmpdir} from 'os';
import {json as runJest} from '../runJest';
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';

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);
});
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.ts
Expand Up @@ -1097,7 +1097,7 @@ class Runtime {

const lastArgs: [Jest | undefined, ...Array<any>] = [
this._config.injectGlobals ? jestObject : undefined, // jest object
this._config.extraGlobals.map<unknown>(globalVariable => {
...this._config.extraGlobals.map<unknown>(globalVariable => {
if (this._environment.global[globalVariable]) {
return this._environment.global[globalVariable];
}
Expand Down

0 comments on commit 395953c

Please sign in to comment.