From dd435fcedb4ecbc28ef4e21e28bcd353b827e020 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 19:25:07 +0530 Subject: [PATCH 1/6] Add mainModule instance variable --- packages/jest-runtime/src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 166c32671e2b..bd936afb5687 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) @@ -1053,6 +1055,15 @@ class Runtime { }), ]; + if (!this._mainModule) { + this._mainModule = module; + } + + Object.defineProperty(module, 'main', { + enumerable: true, + value: this._mainModule, + }); + try { compiledFunction.call( module.exports, From 9db241983e7a60492c3fb0a12d8c61f53b9b363a Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 19:38:34 +0530 Subject: [PATCH 2/6] add `main` to unit tests --- .../jest-runtime/src/__tests__/runtime_require_module.test.js | 2 ++ 1 file changed, 2 insertions(+) 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..a2b19895f124 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -42,6 +42,7 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', + 'main', 'path', 'parent', 'paths', @@ -60,6 +61,7 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', + 'main', 'path', 'parent', 'paths', From fe7a8235b229fa10d46be757091c71aa01274d60 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 19:48:18 +0530 Subject: [PATCH 3/6] update changelog and test --- CHANGELOG.md | 1 + .../jest-runtime/src/__tests__/runtime_require_module.test.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31fc65468fd1..600f86b9ef0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,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/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index a2b19895f124..d9dec727507f 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -42,10 +42,10 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', - 'main', 'path', 'parent', 'paths', + 'main', ]); })); @@ -61,10 +61,10 @@ describe('Runtime requireModule', () => { 'filename', 'id', 'loaded', - 'main', 'path', 'parent', 'paths', + 'main', ]); })); From 615b9c4c57e41014fe98c6fc091cdeedc6f46491 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 20:07:39 +0530 Subject: [PATCH 4/6] reset `mainModule` during teardown --- packages/jest-runtime/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index bd936afb5687..699e93377f1f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -882,6 +882,7 @@ class Runtime { this.resetModules(); this._internalModuleRegistry.clear(); + this._mainModule = null; this._mockFactories.clear(); this._mockMetaDataCache.clear(); this._shouldMockModuleCache.clear(); From d1b488bf638468474d573437c70e88a97e3a92d1 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 20:35:38 +0530 Subject: [PATCH 5/6] add testPath check --- packages/jest-runtime/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 699e93377f1f..875976e5a264 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1056,7 +1056,7 @@ class Runtime { }), ]; - if (!this._mainModule) { + if (!this._mainModule && filename === this._testPath) { this._mainModule = module; } From 59068c48ac66ec5a607d5fb321bfe72fea0a24bd Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Sun, 11 Oct 2020 21:07:13 +0530 Subject: [PATCH 6/6] add integration test --- 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 +++++ 5 files changed, 46 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/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" + } +}