Skip to content

Commit c41420f

Browse files
authored
fix: add require.main when using isolateModules (#10621)
1 parent 5c221d8 commit c41420f

File tree

8 files changed

+61
-0
lines changed

8 files changed

+61
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `[jest-runner, jest-runtime]` fix: `require.main` undefined with `createRequire()` ([#10610](https://github.com/facebook/jest/pull/10610))
1010
- `[jest-runtime]` add missing `module.path` property ([#10615](https://github.com/facebook/jest/pull/10615))
11+
- `[jest-runtime]` fix: add `mainModule` instance variable to runtime ([#10621](https://github.com/facebook/jest/pull/10621))
1112
- `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604))
1213
- `[jest-validate]` Validate `testURL` as CLI option ([#10595](https://github.com/facebook/jest/pull/10595))
1314

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import runJest from '../runJest';
9+
10+
test('`require.main` on using `jest.isolateModules` should not be undefined', () => {
11+
const {exitCode} = runJest('require-main-isolate-modules');
12+
13+
expect(exitCode).toBe(0);
14+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
let foo;
8+
9+
jest.isolateModules(() => (foo = require('../index')));
10+
11+
test('`require.main` on using `jest.isolateModules` should not be undefined', () => {
12+
expect(foo()).toEqual(1);
13+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
module.exports = 1;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
module.exports = () => require.main.require('../child');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

packages/jest-runtime/src/__tests__/runtime_require_module.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('Runtime requireModule', () => {
4545
'path',
4646
'parent',
4747
'paths',
48+
'main',
4849
]);
4950
}));
5051

@@ -63,6 +64,7 @@ describe('Runtime requireModule', () => {
6364
'path',
6465
'parent',
6566
'paths',
67+
'main',
6668
]);
6769
}));
6870

packages/jest-runtime/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class Runtime {
150150
| null;
151151
private _internalModuleRegistry: ModuleRegistry;
152152
private _isCurrentlyExecutingManualMock: string | null;
153+
private _mainModule: Module | null;
153154
private _mockFactories: Map<string, () => unknown>;
154155
private _mockMetaDataCache: Map<
155156
string,
@@ -202,6 +203,7 @@ class Runtime {
202203
this._explicitShouldMock = new Map();
203204
this._internalModuleRegistry = new Map();
204205
this._isCurrentlyExecutingManualMock = null;
206+
this._mainModule = null;
205207
this._mockFactories = new Map();
206208
this._mockRegistry = new Map();
207209
// during setup, this cannot be null (and it's fine to explode if it is)
@@ -891,6 +893,7 @@ class Runtime {
891893
this.resetModules();
892894

893895
this._internalModuleRegistry.clear();
896+
this._mainModule = null;
894897
this._mockFactories.clear();
895898
this._mockMetaDataCache.clear();
896899
this._shouldMockModuleCache.clear();
@@ -1064,6 +1067,15 @@ class Runtime {
10641067
}),
10651068
];
10661069

1070+
if (!this._mainModule && filename === this._testPath) {
1071+
this._mainModule = module;
1072+
}
1073+
1074+
Object.defineProperty(module, 'main', {
1075+
enumerable: true,
1076+
value: this._mainModule,
1077+
});
1078+
10671079
try {
10681080
compiledFunction.call(
10691081
module.exports,

0 commit comments

Comments
 (0)