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

Do not include from information when its not valid #5972

Merged
merged 3 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
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 thier own parent
([#5235](https://github.com/facebook/jest/issues/5235))
* `[expect]` Add support for async matchers
([#5836](https://github.com/facebook/jest/pull/5919))
* `[expect]` Suggest toContainEqual
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