From 395953cc12d4c884de013fbcf25d5d10ac4f52f8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Oct 2020 20:46:15 +0100 Subject: [PATCH] fix: properly inject `extraGlobals` into the environment --- e2e/__tests__/extraGlobals.test.ts | 34 ++++++++++++++++++++++++++++++ packages/jest-runtime/src/index.ts | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 e2e/__tests__/extraGlobals.test.ts diff --git a/e2e/__tests__/extraGlobals.test.ts b/e2e/__tests__/extraGlobals.test.ts new file mode 100644 index 000000000000..c33cadd15a05 --- /dev/null +++ b/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); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 8af7c41b0135..698ff384409a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1097,7 +1097,7 @@ class Runtime { const lastArgs: [Jest | undefined, ...Array] = [ this._config.injectGlobals ? jestObject : undefined, // jest object - this._config.extraGlobals.map(globalVariable => { + ...this._config.extraGlobals.map(globalVariable => { if (this._environment.global[globalVariable]) { return this._environment.global[globalVariable]; }