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: add require.main when using isolateModules #10621

Merged
merged 6 commits into from
Oct 11, 2020
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;
flozender marked this conversation as resolved.
Show resolved Hide resolved
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;
flozender marked this conversation as resolved.
Show resolved Hide resolved
}

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

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