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 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions e2e/__tests__/requireMainResetModules.test.ts
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.
*/

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);
});
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"
}
}
10 changes: 5 additions & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,6 @@ class Runtime {
}),
];

if (!this._mainModule && filename === this._testPath) {
this._mainModule = module;
}

Object.defineProperty(module, 'main', {
enumerable: true,
value: this._mainModule,
Expand Down Expand Up @@ -1398,9 +1394,13 @@ class Runtime {
});
})();

if (!this._mainModule && from.filename === this._testPath) {
this._mainModule = module;
}

flozender marked this conversation as resolved.
Show resolved Hide resolved
Object.defineProperty(moduleRequire, 'main', {
enumerable: true,
value: this._testPath ? this._moduleRegistry.get(this._testPath) : null,
value: this._mainModule,
});
return moduleRequire;
}
Expand Down