diff --git a/CHANGELOG.md b/CHANGELOG.md index 324d27b4caa8..3c7ec91de918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` `require.main` is no longer `undefined` when using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626)) + ### Chore & Maintenance ### Performance diff --git a/e2e/__tests__/requireMainResetModules.test.ts b/e2e/__tests__/requireMainResetModules.test.ts new file mode 100644 index 000000000000..d12459c99ec4 --- /dev/null +++ b/e2e/__tests__/requireMainResetModules.test.ts @@ -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. + */ + +import runJest from '../runJest'; + +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(); +}); 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" + } +} 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', diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b8c2fee5da1f..b19b0dbf1cce 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1400,8 +1400,9 @@ class Runtime { Object.defineProperty(moduleRequire, 'main', { enumerable: true, - value: this._testPath ? this._moduleRegistry.get(this._testPath) : null, + value: this._mainModule, }); + return moduleRequire; }