Skip to content

Commit

Permalink
node-haste2 usage inside of Jest is pivoting!
Browse files Browse the repository at this point in the history
* Resolving the entire Graph for every single test file has proven to be unfeasible in a sufficiently large codebase. This change now circumvents all static resolution and falls back to the same dynamic resolution that node-haste1 was providing in Jest. Jest pulls out the entire HasteMap of node-haste2 and uses that. node-modules are resolved inside of Jest only.
* Instead of providing local moduleMaps per test, the entire haste map is now written to disk and read by workers and shared with all tests.
* Jest is now by default using graceful-fs and provides it to test files as well. This is safer to not run into the file limits
  • Loading branch information
cpojer committed Feb 5, 2016
1 parent f381030 commit b6eb94c
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 308 deletions.
2 changes: 1 addition & 1 deletion bin/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const fs = require('fs');
const fs = require('graceful-fs');
const optimist = require('optimist');
const path = require('path');

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"chalk": "^1.1.1",
"cover": "^0.2.9",
"diff": "^2.1.1",
"graceful-fs": "^4.1.2",
"graceful-fs": "^4.1.3",
"istanbul": "^0.4.2",
"jsdom": "^7.2.0",
"json-stable-stringify": "^1.0.0",
"lodash.template": "^3.6.2",
"mkdirp": "^0.5.1",
"node-haste": "2.0.0-alpha16",
"optimist": "^0.6.1",
"resolve": "^1.1.6",
Expand Down
43 changes: 33 additions & 10 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const mockParentModule = {
exports: {},
};

const normalizedIDCache = Object.create(null);
const moduleNameCache = Object.create(null);

const isFile = file => {
let stat;
try {
Expand Down Expand Up @@ -52,8 +55,8 @@ class Loader {
this._shouldAutoMock = true;
this._configShouldMockModuleNames = Object.create(null);
this._extensions = config.moduleFileExtensions.map(ext => '.' + ext);
this._resolvedModules = moduleMap.resolvedModules;
this._resources = moduleMap.resources;

this._modules = moduleMap.modules;
this._mocks = moduleMap.mocks;

if (config.collectCoverage) {
Expand Down Expand Up @@ -366,14 +369,16 @@ class Loader {

_resolveModuleName(currPath, moduleName) {
// Check if the resolver knows about this module
if (
this._resolvedModules[currPath] &&
this._resolvedModules[currPath][moduleName]
) {
return this._resolvedModules[currPath][moduleName];
if (this._modules[moduleName]) {
return this._modules[moduleName];
} else {
// Otherwise it is likely a node_module.
return this._resolveNodeModule(currPath, moduleName);
const key = currPath + ' : ' + moduleName;
if (moduleNameCache[key]) {
return moduleNameCache[key];
}
moduleNameCache[key] = this._resolveNodeModule(currPath, moduleName);
return moduleNameCache[key];
}
}

Expand All @@ -388,6 +393,17 @@ class Loader {
readFileSync: fs.readFileSync,
});
} catch (e) {
const parts = moduleName.split('/');
const nodeModuleName = parts.shift();
const module = this._getModule(nodeModuleName);
if (module) {
try {
return require.resolve(
path.join.apply(path, [path.dirname(module)].concat(parts))
);
} catch (ignoredError) {}
}

// resolve.sync uses the basedir instead of currPath and therefore
// doesn't throw an accurate error message.
const relativePath = path.relative(basedir, currPath);
Expand All @@ -398,7 +414,7 @@ class Loader {
}

_getModule(resourceName) {
return this._resources[resourceName];
return this._modules[resourceName];
}

_getMockModule(resourceName) {
Expand All @@ -413,6 +429,11 @@ class Loader {
}

_getNormalizedModuleID(currPath, moduleName) {
const key = currPath + ' : ' + moduleName;
if (normalizedIDCache[key]) {
return normalizedIDCache[key];
}

let moduleType;
let mockAbsPath = null;
let realAbsPath = null;
Expand Down Expand Up @@ -459,7 +480,9 @@ class Loader {
}

const delimiter = path.delimiter;
return moduleType + delimiter + realAbsPath + delimiter + mockAbsPath;
const id = moduleType + delimiter + realAbsPath + delimiter + mockAbsPath;
normalizedIDCache[key] = id;
return id;
}

_shouldMock(currPath, moduleName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('nodeHasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('HasteModuleLoader', function() {
function buildLoader() {
const environment = new JSDOMEnvironment(config);
const resolver = new HasteResolver(config, {resetCache: false});
return resolver.getDependencies(rootPath).then(
return resolver.getHasteMap().then(
response => resolver.end().then(() =>
new HasteModuleLoader(config, environment, response)
)
Expand Down
68 changes: 0 additions & 68 deletions src/HasteModuleLoader/__tests__/HasteResolver-Cache-test.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const Console = require('./Console');

class Test {

constructor(path, moduleMap, config) {
constructor(path, config, moduleMap) {
this._path = path;
this._moduleMap = moduleMap;
this._config = config;
this._moduleMap = moduleMap;
}

run() {
const path = this._path;
const moduleMap = this._moduleMap;
const config = this._config;
const moduleMap = this._moduleMap;
const TestEnvironment = require(config.testEnvironment);
const TestRunner = require(config.testRunner);
const ModuleLoader = require(config.moduleLoader);
Expand Down
Loading

0 comments on commit b6eb94c

Please sign in to comment.