From f88382d40e5272a6cbf920396b78b2896760e93b Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 00:11:02 +0530 Subject: [PATCH 1/7] add e2e tests --- e2e/__tests__/requireMainResetModules.test.ts | 15 +++++++++++++++ .../__tests__/index.test.js | 15 +++++++++++++++ e2e/require-main-reset-modules/index.js | 15 +++++++++++++++ e2e/require-main-reset-modules/package.json | 5 +++++ 4 files changed, 50 insertions(+) create mode 100644 e2e/__tests__/requireMainResetModules.test.ts create mode 100644 e2e/require-main-reset-modules/__tests__/index.test.js create mode 100644 e2e/require-main-reset-modules/index.js create mode 100644 e2e/require-main-reset-modules/package.json diff --git a/e2e/__tests__/requireMainResetModules.test.ts b/e2e/__tests__/requireMainResetModules.test.ts new file mode 100644 index 000000000000..aa6a7668c6bf --- /dev/null +++ b/e2e/__tests__/requireMainResetModules.test.ts @@ -0,0 +1,15 @@ +/** + * 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.resetModules` should not be undefined', () => { + const {exitCode} = runJest('require-main-reset-modules', [ + `--resetModules='true'`, + ]); + expect(exitCode).toBe(0); +}); diff --git a/e2e/require-main-reset-modules/__tests__/index.test.js b/e2e/require-main-reset-modules/__tests__/index.test.js new file mode 100644 index 000000000000..2034c089b329 --- /dev/null +++ b/e2e/require-main-reset-modules/__tests__/index.test.js @@ -0,0 +1,15 @@ +/** + * 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. + */ +test('require.main is set', () => { + const {getMain} = require('../index.js'); + expect(getMain()).toBeTruthy(); +}); + +test('require from main works', () => { + const {requireFromMain} = require('../index.js'); + expect(requireFromMain('../package.json')).toBeTruthy(); +}); diff --git a/e2e/require-main-reset-modules/index.js b/e2e/require-main-reset-modules/index.js new file mode 100644 index 000000000000..384d02c8d4b7 --- /dev/null +++ b/e2e/require-main-reset-modules/index.js @@ -0,0 +1,15 @@ +/** + * 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. + */ +Object.assign(exports, {getMain, requireFromMain}); + +function getMain() { + return require.main; +} + +function requireFromMain(pkg) { + return getMain().require(pkg); +} diff --git a/e2e/require-main-reset-modules/package.json b/e2e/require-main-reset-modules/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/require-main-reset-modules/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} From c5b54539efdc54eb07d049a07567d23effc7b054 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 01:02:41 +0530 Subject: [PATCH 2/7] change `main` to use `this._mainModule` --- packages/jest-runtime/src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b8c2fee5da1f..05033b8eae30 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1061,10 +1061,6 @@ class Runtime { }), ]; - if (!this._mainModule && filename === this._testPath) { - this._mainModule = module; - } - Object.defineProperty(module, 'main', { enumerable: true, value: this._mainModule, @@ -1398,9 +1394,13 @@ class Runtime { }); })(); + if (!this._mainModule && from.filename === this._testPath) { + this._mainModule = module; + } + Object.defineProperty(moduleRequire, 'main', { enumerable: true, - value: this._testPath ? this._moduleRegistry.get(this._testPath) : null, + value: this._mainModule, }); return moduleRequire; } From a793db37abbc696750dc4894b58766c1c90fe63e Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 02:44:48 +0530 Subject: [PATCH 3/7] replace mainModule init --- packages/jest-runtime/src/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 05033b8eae30..b19b0dbf1cce 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1061,6 +1061,10 @@ class Runtime { }), ]; + if (!this._mainModule && filename === this._testPath) { + this._mainModule = module; + } + Object.defineProperty(module, 'main', { enumerable: true, value: this._mainModule, @@ -1394,14 +1398,11 @@ class Runtime { }); })(); - if (!this._mainModule && from.filename === this._testPath) { - this._mainModule = module; - } - Object.defineProperty(moduleRequire, 'main', { enumerable: true, value: this._mainModule, }); + return moduleRequire; } From d1ff202808565b2e67b3f52c32a7f7578c51720e Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 02:47:18 +0530 Subject: [PATCH 4/7] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 324d27b4caa8..8d75bf96fd0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` fix: `require.main` is undefined on using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626)) + ### Chore & Maintenance ### Performance From f675da74016ebeaf908ba6fee0575ac9b92c4405 Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 14:00:15 +0530 Subject: [PATCH 5/7] update tests to use `this._mainModule` --- .../jest-runtime/src/__tests__/runtime_require_module.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d9dec727507f..244222b294a1 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -139,7 +139,7 @@ describe('Runtime requireModule', () => { }); it('provides `require.main` to modules', () => createRuntime(__filename).then(runtime => { - runtime._moduleRegistry.set(__filename, module); + runtime._mainModule = module; [ './test_root/modules_with_main/export_main.js', './test_root/modules_with_main/re_export_main.js', From 7526ccacc5b186903658f17334c221b2cbcc54bd Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan Date: Tue, 13 Oct 2020 15:44:44 +0530 Subject: [PATCH 6/7] add tests to use `jest.resetModules()` --- e2e/__tests__/requireMainResetModules.test.ts | 10 +++++++- .../__tests__/callJestResetModules.test.js | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 e2e/require-main-reset-modules/__tests__/callJestResetModules.test.js diff --git a/e2e/__tests__/requireMainResetModules.test.ts b/e2e/__tests__/requireMainResetModules.test.ts index aa6a7668c6bf..d12459c99ec4 100644 --- a/e2e/__tests__/requireMainResetModules.test.ts +++ b/e2e/__tests__/requireMainResetModules.test.ts @@ -7,9 +7,17 @@ import runJest from '../runJest'; -test('`require.main` on using `jest.resetModules` should not be undefined', () => { +test("`require.main` on using `--resetModules='true'` should not be undefined", () => { const {exitCode} = runJest('require-main-reset-modules', [ `--resetModules='true'`, + 'index.test.js', + ]); + expect(exitCode).toBe(0); +}); + +test('`require.main` on using `jest.resetModules()` should not be undefined', () => { + const {exitCode} = runJest('require-main-reset-modules', [ + 'callJestResetModules.test.js', ]); expect(exitCode).toBe(0); }); diff --git a/e2e/require-main-reset-modules/__tests__/callJestResetModules.test.js b/e2e/require-main-reset-modules/__tests__/callJestResetModules.test.js new file mode 100644 index 000000000000..a76b30697219 --- /dev/null +++ b/e2e/require-main-reset-modules/__tests__/callJestResetModules.test.js @@ -0,0 +1,23 @@ +/** + * 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. + */ +beforeEach(() => { + jest.resetModules(); +}); + +afterEach(() => { + jest.resetModules(); +}); + +test('require.main is set', () => { + const {getMain} = require('../index.js'); + expect(getMain()).toBeTruthy(); +}); + +test('require from main works', () => { + const {requireFromMain} = require('../index.js'); + expect(requireFromMain('../package.json')).toBeTruthy(); +}); From 28d06f0ba4b2395bed8944bd045c7afe5dd4b4ef Mon Sep 17 00:00:00 2001 From: Tayeeb Hasan <40818234+flozender@users.noreply.github.com> Date: Tue, 13 Oct 2020 16:13:43 +0530 Subject: [PATCH 7/7] Update CHANGELOG.md Co-authored-by: Simen Bekkhus --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d75bf96fd0e..3c7ec91de918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-runtime]` fix: `require.main` is undefined on using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626)) +- `[jest-runtime]` `require.main` is no longer `undefined` when using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626)) ### Chore & Maintenance