Skip to content

Commit

Permalink
fix: add require.main when using isolateModules (#10621)
Browse files Browse the repository at this point in the history
  • Loading branch information
flozender committed Oct 11, 2020
1 parent 5c221d8 commit c41420f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
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
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 @@ -891,6 +893,7 @@ class Runtime {
this.resetModules();

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

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

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

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

0 comments on commit c41420f

Please sign in to comment.