Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Runtime requireModule', () => {
'path',
'parent',
'paths',
'main',
]);
}));

Expand All @@ -63,6 +64,7 @@ describe('Runtime requireModule', () => {
'path',
'parent',
'paths',
'main',
]);
}));

Expand Down
11 changes: 11 additions & 0 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class Runtime {
| null;
private _internalModuleRegistry: ModuleRegistry;
private _isCurrentlyExecutingManualMock: string | null;
private _mainModule: Module | null;
private _mockFactories: Map<string, () => unknown>;
private _mockMetaDataCache: Map<
string,
Expand Down Expand Up @@ -202,6 +203,7 @@ class Runtime {
this._explicitShouldMock = new Map();
this._internalModuleRegistry = new Map();
this._isCurrentlyExecutingManualMock = null;
this._mainModule = null;
this._mockFactories = new Map();
this._mockRegistry = new Map();
// during setup, this cannot be null (and it's fine to explode if it is)
Expand Down Expand Up @@ -1053,6 +1055,15 @@ class Runtime {
}),
];

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

Object.defineProperty(module, 'main', {
enumerable: true,
value: this._mainModule,
});

try {
compiledFunction.call(
module.exports,
Expand Down