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

Split internal module registry from main module registry #1572

Merged
merged 3 commits into from
Sep 2, 2016
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

const runJest = require('../runJest');

describe('Runtime Internal Module Registry', () => {
// Previously, if Jest required a module (e.g. requiring `mkdirp` from
// `jest-util`) and the project *using* Jest also required that module, Jest
// wouldn't re-require it and thus ignored any mocks that the module may have
// used.
//
// This test verifies that that behavior doesn't happen anymore, and correctly
// uses two module registries: an internal registry that's used specificly by
// Jest to require any internal modules used when setting up the test
// environment, and a "normal" module registry that's used by the actual test
// code (and can safely be cleared after every test)
it('correctly makes use of internal module registry when requiring modules',
() => {
const {json} = runJest.json('runtime-internal-module-registry', []);

expect(json.numTotalTests).toBe(1);
expect(json.numPassedTests).toBe(1);
}
);
});
25 changes: 25 additions & 0 deletions integration_tests/runtime-internal-module-registry/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const fs = jest.genMockFromModule('fs');

let mkdirWasCalled = false;

function mkdirSync() {
mkdirWasCalled = true;
return;
}

fs.mkdirSync = mkdirSync;
fs.__wasMkdirCalled = function() {
return mkdirWasCalled;
};

module.exports = fs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

jest.mock('fs');

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
// transitive deps) being installed along side a projects deps (e.g. with an
// NPM3 flat dep tree)
const jestUtil = require('../../../packages/jest-util');

// If FS is mocked correctly, this folder won't actually be created on the
// filesystem
jestUtil.createDirectory('./dont-create-this-folder');

expect(fs.__wasMkdirCalled()).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
12 changes: 9 additions & 3 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Runtime {
_mockRegistry: {[key: string]: any};
_mocksPattern: ?RegExp;
_moduleRegistry: {[key: string]: Module};
_internalModuleRegistry: {[key: string]: Module};
_resolver: Resolver;
_shouldAutoMock: boolean;
_shouldMockModuleCache: BooleanObject;
Expand All @@ -96,6 +97,7 @@ class Runtime {
resolver: Resolver,
) {
this._moduleRegistry = Object.create(null);
this._internalModuleRegistry = Object.create(null);
this._mockRegistry = Object.create(null);
this._config = config;
this._environment = environment;
Expand Down Expand Up @@ -225,6 +227,10 @@ class Runtime {
const moduleID = this._normalizeID(from, moduleName);
let modulePath;

const moduleRegistry = (!options || !options.isInternalModule) ?
this._moduleRegistry :
this._internalModuleRegistry;

// Some old tests rely on this mocking behavior. Ideally we'll change this
// to be more explicit.
const moduleResource = moduleName && this._resolver.getModule(moduleName);
Expand All @@ -249,15 +255,15 @@ class Runtime {
modulePath = this._resolveModule(from, moduleName);
}

if (!this._moduleRegistry[modulePath]) {
if (!moduleRegistry[modulePath]) {
// We must register the pre-allocated module object first so that any
// circular dependencies that may arise while evaluating the module can
// be satisfied.
const localModule = {
filename: modulePath,
exports: {},
};
this._moduleRegistry[modulePath] = localModule;
moduleRegistry[modulePath] = localModule;
if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
fs.readFileSync(modulePath, 'utf8'),
Expand All @@ -269,7 +275,7 @@ class Runtime {
this._execModule(localModule, options);
}
}
return this._moduleRegistry[modulePath].exports;
return moduleRegistry[modulePath].exports;
}

requireInternalModule(from: Path, to?: string) {
Expand Down