Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
fix(jest-runtime): make sure a module can never be its own parent
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 6, 2018
1 parent e776fdd commit a919713
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
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) {
return null;
}

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

0 comments on commit a919713

Please sign in to comment.