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: do not inject global variable into module wrapper #10644

Merged
merged 1 commit into from Nov 4, 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 @@ -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))

### Chore & Maintenance
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the need for this makes it a breaking change as environments haven't had to do this before


// 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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, as I wrote in an edit in the OP - it breaks for JSDOM as that does not have this code

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);
Copy link
Member Author

@SimenB SimenB Oct 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random fix - we wanna insert the module into the cache ASAP so we don't create multiple. The cache should only have promises anyways, so this was just an oversight (and would have been a type error if the ESM vm APIs had been typed)

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