Skip to content

Commit

Permalink
fix: do not inject global variable into module wrapper (#10644)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 4, 2020
1 parent c98b220 commit a82babd
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))

Expand Down
12 changes: 12 additions & 0 deletions e2e/__tests__/global-mutation.test.ts
@@ -0,0 +1,12 @@
/**
* 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.
*/

const global = 'woo!';

test('can redefine global', () => {
expect(global).toBe('woo!');
});
3 changes: 3 additions & 0 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -45,6 +45,9 @@ class JSDOMEnvironment implements JestEnvironment {
throw new Error('JSDOM did not return a Window object');
}

// for "universal" code (code should use `globalThis`)
global.global = global;

// In the `jsdom@16`, ArrayBuffer was not added to Window, ref: https://github.com/jsdom/jsdom/commit/3a4fd6258e6b13e9cf8341ddba60a06b9b5c7b5b
// Install ArrayBuffer to Window to fix it. Make sure the test is passed, ref: https://github.com/facebook/jest/pull/7626
global.ArrayBuffer = ArrayBuffer;
Expand Down
1 change: 0 additions & 1 deletion packages/jest-environment/src/index.ts
Expand Up @@ -29,7 +29,6 @@ export type ModuleWrapper = (
require: Module['require'],
__dirname: string,
__filename: Module['filename'],
global: Global.Global,
jest?: Jest,
...extraGlobals: Array<Global.Global[keyof Global.Global]>
) => unknown;
Expand Down
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Runtime wrapCodeInModuleWrapper generates the correct args for the module wrapper 1`] = `
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){module.exports = "Hello!"
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){module.exports = "Hello!"
}});
`;

exports[`Runtime wrapCodeInModuleWrapper injects "extra globals" 1`] = `
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest,Math){module.exports = "Hello!"
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest,Math){module.exports = "Hello!"
}});
`;
4 changes: 1 addition & 3 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -383,7 +383,7 @@ class Runtime {
invariant(context);

if (this._resolver.isCoreModule(modulePath)) {
const core = await this._importCoreModule(modulePath, context);
const core = this._importCoreModule(modulePath, context);
this._esmoduleRegistry.set(cacheKey, core);
return core;
}
Expand Down Expand Up @@ -1123,7 +1123,6 @@ class Runtime {
module.require, // require implementation
module.path, // __dirname
module.filename, // __filename
this._environment.global, // global object
// @ts-expect-error
...lastArgs.filter(notEmpty),
);
Expand Down Expand Up @@ -1678,7 +1677,6 @@ class Runtime {
'require',
'__dirname',
'__filename',
'global',
this._config.injectGlobals ? 'jest' : undefined,
...this._config.extraGlobals,
].filter(notEmpty);
Expand Down

0 comments on commit a82babd

Please sign in to comment.