Skip to content

Commit

Permalink
Do not include from information when its not valid (jestjs#5235)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stieg committed Apr 12, 2018
1 parent b80dc79 commit a3242ff
Show file tree
Hide file tree
Showing 3 changed files with 14 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 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
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 a3242ff

Please sign in to comment.