Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: require.main is undefined on using jest.resetModules #10626

Merged
merged 7 commits into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions 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);
});
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
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
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