From a919713a2a0fa13958319bbe07cc17d1396d1062 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 6 Jan 2018 12:17:26 +0100 Subject: [PATCH] fix(jest-runtime): make sure a module can never be its own parent Fixes #5235 --- .../__tests__/module_parent_null_in_test.js | 17 +++++++++++++++++ .../__tests__/index.js | 13 +++++++++++++ .../module_parent_null_in_test/package.json | 4 ++++ .../runtime_internal_module_registry.test.js | 2 +- packages/jest-runtime/src/index.js | 4 ++++ 5 files changed, 39 insertions(+), 1 deletion(-) 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/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..0b6365577734 --- /dev/null +++ b/integration_tests/module_parent_null_in_test/package.json @@ -0,0 +1,4 @@ +{ + "jest": { + } +} diff --git a/integration_tests/runtime-internal-module-registry/__tests__/runtime_internal_module_registry.test.js b/integration_tests/runtime-internal-module-registry/__tests__/runtime_internal_module_registry.test.js index 4d7fb144e05c..d9af77223a99 100644 --- a/integration_tests/runtime-internal-module-registry/__tests__/runtime_internal_module_registry.test.js +++ b/integration_tests/runtime-internal-module-registry/__tests__/runtime_internal_module_registry.test.js @@ -13,7 +13,7 @@ describe('Runtime internal module registry', () => { it('behaves correctly when requiring a module that is used by jest internals', () => { const fs = require('fs'); - // We require from this crazy path so that we can mimick Jest (and it's + // We require from this crazy path so that we can mimick Jest (and its // transitive deps) being installed along side a projects deps (e.g. with an // NPM3 flat dep tree) const jestUtil = require('../../../packages/jest-util'); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 9a322f6fc32d..1f9e17bab900 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -509,6 +509,10 @@ class Runtime { ({ enumerable: true, get() { + if (localModule.filename === from) { + return null; + } + return moduleRegistry[from] || null; }, }: Object),