From a919713a2a0fa13958319bbe07cc17d1396d1062 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 6 Jan 2018 12:17:26 +0100 Subject: [PATCH 1/2] 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), From cd0533eb779ad1604231f2de445985e133e96d18 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 6 Jan 2018 12:28:16 +0100 Subject: [PATCH 2/2] docs(changelog): update changelog --- CHANGELOG.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c52cde971e1..6dfb95b32866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ### Fixes -* `[jest-cli]` Treat dumb terminals as noninteractive ([#5237](https://github.com/facebook/jest/pull/5237)) +* `[jest-cli]` Treat dumb terminals as noninteractive + ([#5237](https://github.com/facebook/jest/pull/5237)) * `[jest-cli]` `jest --onlyChanged --changedFilesWithAncestor` now also works with git. ([#5189](https://github.com/facebook/jest/pull/5189)) * `[jest-config]` fix unexpected condition to avoid infinite recursion in @@ -11,6 +12,8 @@ ([#5230](https://github.com/facebook/jest/pull/5230)) * `[expect]` Do not override `Error` stack (with `Error.captureStackTrace`) for custom matchers. ([#5162](https://github.com/facebook/jest/pull/5162)) +* `[jest-runtime]` Make sure a module cannot be its own parent. + ([#5241](https://github.com/facebook/jest/pull/5241)) ### Features @@ -29,14 +32,15 @@ * `[jest-runner]` test environments are now passed a new `options` parameter. Currently this only has the `console` which is the test console that Jest will expose to tests. ([#5223](https://github.com/facebook/jest/issues/5223)) -* `[jest-environment-jsdom]` pass the `options.console` to a custom - instance of `virtualConsole` so jsdom is using the same console as the - test. ([#5223](https://github.com/facebook/jest/issues/5223)) +* `[jest-environment-jsdom]` pass the `options.console` to a custom instance of + `virtualConsole` so jsdom is using the same console as the test. + ([#5223](https://github.com/facebook/jest/issues/5223)) ### Chore & Maintenance * `[docs]` Describe the order of execution of describe and test blocks. - ([#5217](https://github.com/facebook/jest/pull/5217), [#5238](https://github.com/facebook/jest/pull/5238)) + ([#5217](https://github.com/facebook/jest/pull/5217), + [#5238](https://github.com/facebook/jest/pull/5238)) ## jest 22.0.4