Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-runtime): make sure a module can never be its own parent #5241

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions integration_tests/__tests__/module_parent_null_in_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
13 changes: 13 additions & 0 deletions integration_tests/module_parent_null_in_test/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
4 changes: 4 additions & 0 deletions integration_tests/module_parent_null_in_test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jest": {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ class Runtime {
({
enumerable: true,
get() {
if (localModule.filename === from) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had moduleRegistry[from] === localModule, but I think it's the same thing in practice

return null;
}

return moduleRegistry[from] || null;
},
}: Object),
Expand Down