Skip to content

Commit e393469

Browse files
stiegcpojer
authored andcommitted
Do not include from information when its not valid (#5972)
* Do not include `from` information when its not valid (#5235) If you include the information then when the _execModule routine loads the module without a from context then it will incorrectly setup a circular dependency by declaring the parent is itself. By checking for a module name and not including that information when passed in, the issue is avoided. * Copy in integrations tests from pull #5241 Done at the request of @SimenB from PR #5972
1 parent d8cc233 commit e393469

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
* `[jest-runtime]` Prevent modules from marking themselves as their own parent
6+
([#5235](https://github.com/facebook/jest/issues/5235))
57
* `[jest-mock]` Add support for auto-mocking generator functions
68
([#5983](https://github.com/facebook/jest/pull/5983))
79
* `[expect]` Add support for async matchers
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
* @flow
8+
*/
9+
'use strict';
10+
11+
const runJest = require('../runJest');
12+
13+
test('module.parent should be null in test files', () => {
14+
const {status} = runJest('module_parent_null_in_test');
15+
16+
expect(status).toBe(0);
17+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
'use strict';
9+
10+
test('moduleNameMapping wrong configuration', () => {
11+
expect(module).not.toBe(module.parent);
12+
expect(module.parent).toBeNull();
13+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jest": {}
3+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ describe('Runtime', () => {
173173
expect(exports.isManualMockModule).toBe(true);
174174
});
175175
});
176+
176177
it('provides `require.main` in mock', () =>
177178
createRuntime(__filename).then(runtime => {
178-
runtime._moduleRegistry[__filename] = module;
179-
runtime.setMock(__filename, 'export_main', () => require.main, {
179+
runtime.setMock(__filename, 'export_main', () => module, {
180180
virtual: true,
181181
});
182182
const mainModule = runtime.requireMock(__filename, 'export_main');

packages/jest-runtime/src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ class Runtime {
327327
// $FlowFixMe
328328
localModule.exports = require(modulePath);
329329
} else {
330-
this._execModule(localModule, options, moduleRegistry, from);
330+
// Only include the fromPath if a moduleName is given. Else treat as root.
331+
const fromPath = moduleName ? from : null;
332+
this._execModule(localModule, options, moduleRegistry, fromPath);
331333
}
332334

333335
localModule.loaded = true;
@@ -392,7 +394,10 @@ class Runtime {
392394
id: modulePath,
393395
loaded: false,
394396
};
395-
this._execModule(localModule, undefined, this._mockRegistry, from);
397+
398+
// Only include the fromPath if a moduleName is given. Else treat as root.
399+
const fromPath = moduleName ? from : null;
400+
this._execModule(localModule, undefined, this._mockRegistry, fromPath);
396401
this._mockRegistry[moduleID] = localModule.exports;
397402
localModule.loaded = true;
398403
} else {
@@ -493,7 +498,7 @@ class Runtime {
493498
localModule: Module,
494499
options: ?InternalModuleOptions,
495500
moduleRegistry: ModuleRegistry,
496-
from: Path,
501+
from: ?Path,
497502
) {
498503
// If the environment was disposed, prevent this module from being executed.
499504
if (!this._environment.global) {
@@ -517,7 +522,8 @@ class Runtime {
517522
({
518523
enumerable: true,
519524
get() {
520-
return moduleRegistry[from] || null;
525+
const key = from || '';
526+
return moduleRegistry[key] || null;
521527
},
522528
}: Object),
523529
);

0 commit comments

Comments
 (0)