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 all 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
14 changes: 14 additions & 0 deletions e2e/__tests__/requireMainIsolateModules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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.isolateModules` should not be undefined', () => {
const {exitCode} = runJest('require-main-isolate-modules');

expect(exitCode).toBe(0);
});
13 changes: 13 additions & 0 deletions e2e/require-main-isolate-modules/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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.
*/
let foo;

jest.isolateModules(() => (foo = require('../index')));

test('`require.main` on using `jest.isolateModules` should not be undefined', () => {
expect(foo()).toEqual(1);
});
7 changes: 7 additions & 0 deletions e2e/require-main-isolate-modules/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 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.
*/
module.exports = 1;
7 changes: 7 additions & 0 deletions e2e/require-main-isolate-modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 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.
*/
module.exports = () => require.main.require('../child');
5 changes: 5 additions & 0 deletions e2e/require-main-isolate-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
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
12 changes: 12 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 @@ -880,6 +882,7 @@ class Runtime {
this.resetModules();

this._internalModuleRegistry.clear();
this._mainModule = null;
this._mockFactories.clear();
this._mockMetaDataCache.clear();
this._shouldMockModuleCache.clear();
Expand Down Expand Up @@ -1053,6 +1056,15 @@ class Runtime {
}),
];

if (!this._mainModule && filename === this._testPath) {
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