Skip to content

Commit

Permalink
Do not include from information when its not valid (#5972)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stieg authored and cpojer committed Apr 15, 2018
1 parent d8cc233 commit e393469
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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();
});
3 changes: 3 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,3 @@
{
"jest": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 10 additions & 4 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -517,7 +522,8 @@ class Runtime {
({
enumerable: true,
get() {
return moduleRegistry[from] || null;
const key = from || '';
return moduleRegistry[key] || null;
},
}: Object),
);
Expand Down

0 comments on commit e393469

Please sign in to comment.