From e393469a61cfd45703ca4342825aab234bbc7464 Mon Sep 17 00:00:00 2001 From: Andrew Stiegmann Date: Sat, 14 Apr 2018 23:30:05 -1000 Subject: [PATCH] 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 --- CHANGELOG.md | 2 ++ .../__tests__/module_parent_null_in_test.js | 17 +++++++++++++++++ .../__tests__/index.js | 13 +++++++++++++ .../module_parent_null_in_test/package.json | 3 +++ .../src/__tests__/runtime_require_mock.test.js | 4 ++-- packages/jest-runtime/src/index.js | 14 ++++++++++---- 6 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 integration-tests/__tests__/module_parent_null_in_test.js create mode 100644 integration-tests/module_parent_null_in_test/__tests__/index.js create mode 100644 integration-tests/module_parent_null_in_test/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index d93b8457f905..fc41295c35a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +* `[jest-runtime]` Prevent modules from marking themselves as their own parent + ([#5235](https://github.com/facebook/jest/issues/5235)) * `[jest-mock]` Add support for auto-mocking generator functions ([#5983](https://github.com/facebook/jest/pull/5983)) * `[expect]` Add support for async matchers diff --git a/integration-tests/__tests__/module_parent_null_in_test.js b/integration-tests/__tests__/module_parent_null_in_test.js new file mode 100644 index 000000000000..0b188ff36950 --- /dev/null +++ b/integration-tests/__tests__/module_parent_null_in_test.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + * + * @flow + */ +'use strict'; + +const runJest = require('../runJest'); + +test('module.parent should be null in test files', () => { + const {status} = runJest('module_parent_null_in_test'); + + expect(status).toBe(0); +}); diff --git a/integration-tests/module_parent_null_in_test/__tests__/index.js b/integration-tests/module_parent_null_in_test/__tests__/index.js new file mode 100644 index 000000000000..27bbab9627b4 --- /dev/null +++ b/integration-tests/module_parent_null_in_test/__tests__/index.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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. + */ + +'use strict'; + +test('moduleNameMapping wrong configuration', () => { + expect(module).not.toBe(module.parent); + expect(module.parent).toBeNull(); +}); diff --git a/integration-tests/module_parent_null_in_test/package.json b/integration-tests/module_parent_null_in_test/package.json new file mode 100644 index 000000000000..586d4ca6b75c --- /dev/null +++ b/integration-tests/module_parent_null_in_test/package.json @@ -0,0 +1,3 @@ +{ + "jest": {} +} diff --git a/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js b/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js index 38dab7d03279..f86118ed963c 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js @@ -173,10 +173,10 @@ describe('Runtime', () => { expect(exports.isManualMockModule).toBe(true); }); }); + it('provides `require.main` in mock', () => createRuntime(__filename).then(runtime => { - runtime._moduleRegistry[__filename] = module; - runtime.setMock(__filename, 'export_main', () => require.main, { + runtime.setMock(__filename, 'export_main', () => module, { virtual: true, }); const mainModule = runtime.requireMock(__filename, 'export_main'); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 8c5619bab736..d179cdfbb81b 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -327,7 +327,9 @@ class Runtime { // $FlowFixMe localModule.exports = require(modulePath); } else { - this._execModule(localModule, options, moduleRegistry, from); + // Only include the fromPath if a moduleName is given. Else treat as root. + const fromPath = moduleName ? from : null; + this._execModule(localModule, options, moduleRegistry, fromPath); } localModule.loaded = true; @@ -392,7 +394,10 @@ class Runtime { id: modulePath, loaded: false, }; - this._execModule(localModule, undefined, this._mockRegistry, from); + + // Only include the fromPath if a moduleName is given. Else treat as root. + const fromPath = moduleName ? from : null; + this._execModule(localModule, undefined, this._mockRegistry, fromPath); this._mockRegistry[moduleID] = localModule.exports; localModule.loaded = true; } else { @@ -493,7 +498,7 @@ class Runtime { localModule: Module, options: ?InternalModuleOptions, moduleRegistry: ModuleRegistry, - from: Path, + from: ?Path, ) { // If the environment was disposed, prevent this module from being executed. if (!this._environment.global) { @@ -517,7 +522,8 @@ class Runtime { ({ enumerable: true, get() { - return moduleRegistry[from] || null; + const key = from || ''; + return moduleRegistry[key] || null; }, }: Object), );