From c41420f3f2c92b48f155cccd6b2eef4ccf11c689 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan <40818234+flozender@users.noreply.github.com> Date: Sun, 11 Oct 2020 22:07:43 +0530 Subject: [PATCH] fix: add `require.main` when using `isolateModules` (#10621) --- CHANGELOG.md | 1 + e2e/__tests__/requireMainIsolateModules.test.ts | 14 ++++++++++++++ .../__tests__/index.test.js | 13 +++++++++++++ e2e/require-main-isolate-modules/child.js | 7 +++++++ e2e/require-main-isolate-modules/index.js | 7 +++++++ e2e/require-main-isolate-modules/package.json | 5 +++++ .../src/__tests__/runtime_require_module.test.js | 2 ++ packages/jest-runtime/src/index.ts | 12 ++++++++++++ 8 files changed, 61 insertions(+) create mode 100644 e2e/__tests__/requireMainIsolateModules.test.ts create mode 100644 e2e/require-main-isolate-modules/__tests__/index.test.js create mode 100644 e2e/require-main-isolate-modules/child.js create mode 100644 e2e/require-main-isolate-modules/index.js create mode 100644 e2e/require-main-isolate-modules/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index edbf9a3442cc..f75ab1c33046 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-runner, jest-runtime]` fix: `require.main` undefined with `createRequire()` ([#10610](https://github.com/facebook/jest/pull/10610)) - `[jest-runtime]` add missing `module.path` property ([#10615](https://github.com/facebook/jest/pull/10615)) +- `[jest-runtime]` fix: add `mainModule` instance variable to runtime ([#10621](https://github.com/facebook/jest/pull/10621)) - `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604)) - `[jest-validate]` Validate `testURL` as CLI option ([#10595](https://github.com/facebook/jest/pull/10595)) diff --git a/e2e/__tests__/requireMainIsolateModules.test.ts b/e2e/__tests__/requireMainIsolateModules.test.ts new file mode 100644 index 000000000000..a924da803da9 --- /dev/null +++ b/e2e/__tests__/requireMainIsolateModules.test.ts @@ -0,0 +1,14 @@ +/** + * 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 runJest from '../runJest'; + +test('`require.main` on using `jest.isolateModules` should not be undefined', () => { + const {exitCode} = runJest('require-main-isolate-modules'); + + expect(exitCode).toBe(0); +}); diff --git a/e2e/require-main-isolate-modules/__tests__/index.test.js b/e2e/require-main-isolate-modules/__tests__/index.test.js new file mode 100644 index 000000000000..c1c4ead60341 --- /dev/null +++ b/e2e/require-main-isolate-modules/__tests__/index.test.js @@ -0,0 +1,13 @@ +/** + * 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. + */ +let foo; + +jest.isolateModules(() => (foo = require('../index'))); + +test('`require.main` on using `jest.isolateModules` should not be undefined', () => { + expect(foo()).toEqual(1); +}); diff --git a/e2e/require-main-isolate-modules/child.js b/e2e/require-main-isolate-modules/child.js new file mode 100644 index 000000000000..79d1c6411a6c --- /dev/null +++ b/e2e/require-main-isolate-modules/child.js @@ -0,0 +1,7 @@ +/** + * 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. + */ +module.exports = 1; diff --git a/e2e/require-main-isolate-modules/index.js b/e2e/require-main-isolate-modules/index.js new file mode 100644 index 000000000000..fbdc93a0e19e --- /dev/null +++ b/e2e/require-main-isolate-modules/index.js @@ -0,0 +1,7 @@ +/** + * 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. + */ +module.exports = () => require.main.require('../child'); diff --git a/e2e/require-main-isolate-modules/package.json b/e2e/require-main-isolate-modules/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/require-main-isolate-modules/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index 7a9702898ebb..d9dec727507f 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -45,6 +45,7 @@ describe('Runtime requireModule', () => { 'path', 'parent', 'paths', + 'main', ]); })); @@ -63,6 +64,7 @@ describe('Runtime requireModule', () => { 'path', 'parent', 'paths', + 'main', ]); })); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 3a7c3b157084..8977865777ce 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -150,6 +150,7 @@ class Runtime { | null; private _internalModuleRegistry: ModuleRegistry; private _isCurrentlyExecutingManualMock: string | null; + private _mainModule: Module | null; private _mockFactories: Map unknown>; private _mockMetaDataCache: Map< string, @@ -202,6 +203,7 @@ class Runtime { this._explicitShouldMock = new Map(); this._internalModuleRegistry = new Map(); this._isCurrentlyExecutingManualMock = null; + this._mainModule = null; this._mockFactories = new Map(); this._mockRegistry = new Map(); // during setup, this cannot be null (and it's fine to explode if it is) @@ -891,6 +893,7 @@ class Runtime { this.resetModules(); this._internalModuleRegistry.clear(); + this._mainModule = null; this._mockFactories.clear(); this._mockMetaDataCache.clear(); this._shouldMockModuleCache.clear(); @@ -1064,6 +1067,15 @@ class Runtime { }), ]; + if (!this._mainModule && filename === this._testPath) { + this._mainModule = module; + } + + Object.defineProperty(module, 'main', { + enumerable: true, + value: this._mainModule, + }); + try { compiledFunction.call( module.exports,