Skip to content

Commit

Permalink
fix: require.main is undefined on using jest.resetModules (#10626)
Browse files Browse the repository at this point in the history
  • Loading branch information
flozender authored Oct 13, 2020
1 parent 591d612 commit 3641e3c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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
Expand Down
23 changes: 23 additions & 0 deletions e2e/__tests__/requireMainResetModules.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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();
});
15 changes: 15 additions & 0 deletions e2e/require-main-reset-modules/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
15 changes: 15 additions & 0 deletions e2e/require-main-reset-modules/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions e2e/require-main-reset-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3641e3c

Please sign in to comment.