'use strict';
var fs = require('graceful-fs');
var hasteLoaders = require('node-haste/lib/loaders');
var moduleMocker = require('../lib/moduleMocker');
var NodeHaste = require('node-haste/lib/Haste');
var os = require('os');
var path = require('path');
var Q = require('q');
var resolve = require('resolve');
var utils = require('../lib/utils');
var COVERAGE_STORAGE_VAR_NAME = '____JEST_COVERAGE_DATA____';
var NODE_PATH = process.env.NODE_PATH;
var IS_PATH_BASED_MODULE_NAME = /^(?:\.\.?\/|\/)/;
var NODE_CORE_MODULES = {
assert: true,
buffer: true,
child_process: true,
cluster: true,
console: true,
constants: true,
crypto: true,
dgram: true,
dns: true,
domain: true,
events: true,
freelist: true,
fs: true,
http: true,
https: true,
module: true,
net: true,
os: true,
path: true,
punycode: true,
querystring: true,
readline: true,
repl: true,
smalloc: true,
stream: true,
string_decoder: true,
sys: true,
timers: true,
tls: true,
tty: true,
url: true,
util: true,
vm: true,
zlib: true
};
var VENDOR_PATH = path.resolve(__dirname, '../../vendor');
var _configUnmockListRegExpCache = null;
function _buildLoadersList(config) {
return [
new hasteLoaders.ProjectConfigurationLoader(),
new hasteLoaders.JSTestLoader(config.setupJSTestLoaderOptions),
new hasteLoaders.JSMockLoader(config.setupJSMockLoaderOptions),
new hasteLoaders.JSLoader(config.setupJSLoaderOptions),
new hasteLoaders.ResourceLoader()
];
}
function _constructHasteInst(config, options) {
var HASTE_IGNORE_REGEX = new RegExp(
config.modulePathIgnorePatterns.length > 0
? config.modulePathIgnorePatterns.join('|')
: '$.'
);
if (!fs.existsSync(config.cacheDirectory)) {
fs.mkdirSync(config.cacheDirectory);
}
return new NodeHaste(
_buildLoadersList(config),
(config.testPathDirs || []),
{
ignorePaths: function(path) {
return path.match(HASTE_IGNORE_REGEX);
},
version: JSON.stringify(config),
useNativeFind: true,
maxProcesses: os.cpus().length,
maxOpenFiles: options.maxOpenFiles || 100
}
);
}
function _getCacheFilePath(config) {
return path.join(config.cacheDirectory, 'cache-' + config.name);
}
function Loader(config, environment, resourceMap) {
this._config = config;
this._CoverageCollector = require(config.coverageCollector);
this._coverageCollectors = {};
this._currentlyExecutingModulePath = '';
this._environment = environment;
this._explicitShouldMock = {};
this._explicitlySetMocks = {};
this._isCurrentlyExecutingManualMock = null;
this._mockMetaDataCache = {};
this._nodeModuleProjectConfigNameToResource = null;
this._resourceMap = resourceMap;
this._reverseDependencyMap = null;
this._shouldAutoMock = true;
this._configShouldMockModuleNames = {};
if (_configUnmockListRegExpCache === null) {
if (typeof WeakMap !== 'function') {
throw new Error(
'Please run node with the --harmony flag! jest requires WeakMap ' +
'which is only available with the --harmony flag in node < v0.12'
);
}
_configUnmockListRegExpCache = new WeakMap();
}
if (!config.unmockedModulePathPatterns
|| config.unmockedModulePathPatterns.length === 0) {
this._unmockListRegExps = [];
} else {
this._unmockListRegExps = _configUnmockListRegExpCache.get(config);
if (!this._unmockListRegExps) {
this._unmockListRegExps = config.unmockedModulePathPatterns
.map(function(unmockPathRe) {
return new RegExp(unmockPathRe);
});
_configUnmockListRegExpCache.set(config, this._unmockListRegExps);
}
}
this.resetModuleRegistry();
}
Loader.loadResourceMap = function(config, options) {
options = options || {};
var deferred = Q.defer();
try {
_constructHasteInst(config, options).update(
_getCacheFilePath(config),
function(resourceMap) {
deferred.resolve(resourceMap);
}
);
} catch (e) {
deferred.reject(e);
}
return deferred.promise;
};
Loader.loadResourceMapFromCacheFile = function(config, options) {
options = options || {};
var deferred = Q.defer();
try {
var hasteInst = _constructHasteInst(config, options);
hasteInst.loadMap(
_getCacheFilePath(config),
function(err, map) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(map);
}
}
);
} catch (e) {
deferred.reject(e);
}
return deferred.promise;
};
Loader.prototype._execModule = function(moduleObj) {
var modulePath = moduleObj.__filename;
var moduleContent =
utils.readAndPreprocessFileContent(modulePath, this._config);
moduleObj.require = this.constructBoundRequire(modulePath);
var moduleLocalBindings = {
'module': moduleObj,
'exports': moduleObj.exports,
'require': moduleObj.require,
'__dirname': path.dirname(modulePath),
'__filename': modulePath,
'global': this._environment.global,
'jest': this._builtInModules['jest-runtime'](modulePath).exports
};
var onlyCollectFrom = this._config.collectCoverageOnlyFrom;
var shouldCollectCoverage =
this._config.collectCoverage === true && !onlyCollectFrom
|| (onlyCollectFrom && onlyCollectFrom[modulePath] === true);
if (shouldCollectCoverage) {
if (!this._coverageCollectors.hasOwnProperty(modulePath)) {
this._coverageCollectors[modulePath] =
new this._CoverageCollector(moduleContent, modulePath);
}
var collector = this._coverageCollectors[modulePath];
moduleLocalBindings[COVERAGE_STORAGE_VAR_NAME] =
collector.getCoverageDataStore();
moduleContent = collector.getInstrumentedSource(COVERAGE_STORAGE_VAR_NAME);
}
var lastExecutingModulePath = this._currentlyExecutingModulePath;
this._currentlyExecutingModulePath = modulePath;
var origCurrExecutingManualMock = this._isCurrentlyExecutingManualMock;
this._isCurrentlyExecutingManualMock = modulePath;
utils.runContentWithLocalBindings(
this._environment.runSourceText.bind(this._environment),
moduleContent,
modulePath,
moduleLocalBindings
);
this._isCurrentlyExecutingManualMock = origCurrExecutingManualMock;
this._currentlyExecutingModulePath = lastExecutingModulePath;
};
Loader.prototype._generateMock = function(currPath, moduleName) {
var modulePath = this._moduleNameToPath(currPath, moduleName);
if (!this._mockMetaDataCache.hasOwnProperty(modulePath)) {
this._mockMetaDataCache[modulePath] = moduleMocker.getMetadata({});
var origMockRegistry = this._mockRegistry;
var origModuleRegistry = this._moduleRegistry;
this._mockRegistry = {};
this._moduleRegistry = {};
var moduleExports = this.requireModule(currPath, moduleName);
this._mockRegistry = origMockRegistry;
this._moduleRegistry = origModuleRegistry;
this._mockMetaDataCache[modulePath] = moduleMocker.getMetadata(
moduleExports
);
}
return moduleMocker.generateFromMetadata(
this._mockMetaDataCache[modulePath]
);
};
Loader.prototype._getDependencyPathsFromResource = function(resource) {
var dependencyPaths = [];
for (var i = 0; i < resource.requiredModules.length; i++) {
var requiredModule = resource.requiredModules[i];
if (resource.getModuleIDByOrigin) {
requiredModule =
resource.getModuleIDByOrigin(requiredModule) || requiredModule;
}
try {
var moduleID = this._getNormalizedModuleID(resource.path, requiredModule);
} catch(e) {
console.warn(
'Could not find a `' + requiredModule + '` module while analyzing ' +
'dependencies of `' + resource.id + '`'
);
continue;
}
dependencyPaths.push(this._getRealPathFromNormalizedModuleID(moduleID));
}
return dependencyPaths;
};
Loader.prototype._getResource = function(resourceType, resourceName) {
var resource = this._resourceMap.getResource(resourceType, resourceName);
if (resource === undefined && resourceType === 'JS' && /\//.test(resourceName)
&& !/\.js$/.test(resourceName)) {
resource = this._resourceMap.getResource(
resourceType,
resourceName + '.js'
);
}
return resource;
};
Loader.prototype._getNormalizedModuleID = function(currPath, moduleName) {
var moduleType;
var mockAbsPath = null;
var realAbsPath = null;
if (this._builtInModules.hasOwnProperty(moduleName)) {
moduleType = 'builtin';
realAbsPath = moduleName;
} else if (NODE_CORE_MODULES.hasOwnProperty(moduleName)) {
moduleType = 'node';
realAbsPath = moduleName;
} else {
moduleType = 'user';
if (IS_PATH_BASED_MODULE_NAME.test(moduleName)
|| (this._getResource('JS', moduleName) === undefined
&& this._getResource('JSMock', moduleName) === undefined)) {
var absolutePath = this._moduleNameToPath(currPath, moduleName);
if (absolutePath === undefined) {
throw new Error(
'Cannot find module \'' + moduleName + '\' from \'' + currPath + '\''
);
}
var resource = this._resourceMap.getResourceByPath(absolutePath);
if (resource) {
if (resource.type === 'JS') {
realAbsPath = absolutePath;
} else if (resource.type === 'JSMock') {
mockAbsPath = absolutePath;
}
moduleName = resource.id;
}
}
if (realAbsPath === null) {
var moduleResource = this._getResource('JS', moduleName);
if (moduleResource) {
realAbsPath = moduleResource.path;
}
}
if (mockAbsPath === null) {
var mockResource = this._getResource('JSMock', moduleName);
if (mockResource) {
mockAbsPath = mockResource.path;
}
}
}
return [moduleType, realAbsPath, mockAbsPath].join(':');
};
Loader.prototype._getRealPathFromNormalizedModuleID = function(moduleID) {
return moduleID.split(':')[1];
};
Loader.prototype._moduleNameToPath = function(currPath, moduleName) {
if (this._builtInModules.hasOwnProperty(moduleName)) {
return moduleName;
}
if (IS_PATH_BASED_MODULE_NAME.test(moduleName)) {
var modulePath = path.resolve(currPath, '..', moduleName);
var ext, i;
var extensions = this._config.moduleFileExtensions;
if (fs.existsSync(modulePath) &&
fs.statSync(modulePath).isFile()) {
return modulePath;
}
for (i = 0; i < extensions.length; i++) {
ext = '.' + extensions[i];
if (fs.existsSync(modulePath + ext) &&
fs.statSync(modulePath + ext).isFile()) {
return modulePath + ext;
}
}
if (fs.existsSync(modulePath) &&
fs.statSync(modulePath).isDirectory()) {
var packagePath = path.join(modulePath, 'package.json');
if (fs.existsSync(packagePath)) {
var mainPath = path.join(modulePath, require(packagePath).main);
if (fs.existsSync(mainPath)) {
return mainPath;
}
}
var indexPath = path.join(modulePath, 'index');
for (i = 0; i < extensions.length; i++) {
ext = '.' + extensions[i];
if (fs.existsSync(indexPath + ext) &&
fs.statSync(indexPath + ext).isFile()) {
return indexPath + ext;
}
}
}
} else {
var resource = this._getResource('JS', moduleName);
if (!resource) {
return this._nodeModuleNameToPath(
currPath,
moduleName
);
}
return resource.path;
}
};
Loader.prototype._nodeModuleNameToPath = function(currPath, moduleName) {
var subModulePath = null;
var moduleProjectPart = moduleName;
if (/\//.test(moduleName)) {
var projectPathParts = moduleName.split('/');
moduleProjectPart = projectPathParts.shift();
subModulePath = projectPathParts.join('/');
}
var resolveError = null;
var exts = this._config.moduleFileExtensions
.map(function(ext){
return '.' + ext;
});
try {
if (NODE_PATH) {
return resolve.sync(moduleName, {
paths: NODE_PATH.split(path.delimiter),
basedir: path.dirname(currPath),
extensions: exts
});
} else {
return resolve.sync(moduleName, {
basedir: path.dirname(currPath),
extensions: exts
});
}
} catch (e) {
resolveError = e;
}
if (this._nodeModuleProjectConfigNameToResource === null) {
this._nodeModuleProjectConfigNameToResource = {};
var resources =
this._resourceMap.getAllResourcesByType('ProjectConfiguration');
resources.forEach(function(res) {
this._nodeModuleProjectConfigNameToResource[res.data.name] = res;
}.bind(this));
}
var resource = this._nodeModuleProjectConfigNameToResource[moduleProjectPart];
if (!resource) {
throw resolveError;
}
var resourceDirname = path.dirname(resource.path);
var currFileDirname = path.dirname(currPath);
if (resourceDirname.indexOf(currFileDirname) > 0) {
throw resolveError;
}
if (subModulePath === null) {
subModulePath =
resource.data.hasOwnProperty('main')
? resource.data.main
: 'index.js';
}
return this._moduleNameToPath(
resource.path,
'./' + subModulePath
);
};
Loader.prototype._shouldMock = function(currPath, moduleName) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
if (this._builtInModules.hasOwnProperty(moduleName)) {
return false;
} else if (this._explicitShouldMock.hasOwnProperty(moduleID)) {
return this._explicitShouldMock[moduleID];
} else if (NODE_CORE_MODULES[moduleName]) {
return false;
} else if (this._shouldAutoMock) {
if (this._configShouldMockModuleNames.hasOwnProperty(moduleName)) {
return this._configShouldMockModuleNames[moduleName];
} else if (this._unmockListRegExps.length > 0) {
this._configShouldMockModuleNames[moduleName] = true;
var manualMockResource =
this._getResource('JSMock', moduleName);
try {
var modulePath = this._moduleNameToPath(currPath, moduleName);
} catch(e) {
if (manualMockResource) {
return true;
}
throw e;
}
var unmockRegExp;
if (modulePath.indexOf(VENDOR_PATH) === 0) {
return false;
}
this._configShouldMockModuleNames[moduleName] = true;
for (var i = 0; i < this._unmockListRegExps.length; i++) {
unmockRegExp = this._unmockListRegExps[i];
if (unmockRegExp.test(modulePath)) {
return this._configShouldMockModuleNames[moduleName] = false;
}
}
return this._configShouldMockModuleNames[moduleName];
}
return true;
} else {
return false;
}
};
Loader.prototype.constructBoundRequire = function(sourceModulePath) {
var boundModuleRequire = this.requireModuleOrMock.bind(
this,
sourceModulePath
);
boundModuleRequire.resolve = function(moduleName) {
var ret = this._moduleNameToPath(sourceModulePath, moduleName);
if (!ret) {
throw new Error('Module(' + moduleName + ') not found!');
}
return ret;
}.bind(this);
boundModuleRequire.generateMock = this._generateMock.bind(
this,
sourceModulePath
);
boundModuleRequire.requireMock = this.requireMock.bind(
this,
sourceModulePath
);
boundModuleRequire.requireActual = this.requireModule.bind(
this,
sourceModulePath
);
return boundModuleRequire;
};
Loader.prototype.getAllCoverageInfo = function() {
if (!this._config.collectCoverage) {
throw new Error(
'config.collectCoverage was not set, so no coverage info has been ' +
'(or will be) collected!'
);
}
var coverageInfo = {};
for (var filePath in this._coverageCollectors) {
coverageInfo[filePath] =
this._coverageCollectors[filePath].extractRuntimeCoverageInfo();
}
return coverageInfo;
};
Loader.prototype.getCoverageForFilePath = function(filePath) {
if (!this._config.collectCoverage) {
throw new Error(
'config.collectCoverage was not set, so no coverage info has been ' +
'(or will be) collected!'
);
}
return (
this._coverageCollectors.hasOwnProperty(filePath)
? this._coverageCollectors[filePath].extractRuntimeCoverageInfo()
: null
);
};
Loader.prototype.getDependenciesFromPath = function(modulePath) {
var resource = this._resourceMap.getResourceByPath(modulePath);
if (!resource) {
throw new Error('Unknown modulePath: ' + modulePath);
}
if (resource.type === 'ProjectConfiguration'
|| resource.type === 'Resource') {
throw new Error(
'Could not extract dependency information from this type of file!'
);
}
return this._getDependencyPathsFromResource(resource);
};
Loader.prototype.getDependentsFromPath = function(modulePath) {
if (this._reverseDependencyMap === null) {
var resourceMap = this._resourceMap;
var reverseDepMap = this._reverseDependencyMap = {};
var allResources = resourceMap.getAllResources();
for (var resourceID in allResources) {
var resource = allResources[resourceID];
if (resource.type === 'ProjectConfiguration'
|| resource.type === 'Resource') {
continue;
}
var dependencyPaths = this._getDependencyPathsFromResource(resource);
for (var i = 0; i < dependencyPaths.length; i++) {
var requiredModulePath = dependencyPaths[i];
if (!reverseDepMap.hasOwnProperty(requiredModulePath)) {
reverseDepMap[requiredModulePath] = {};
}
reverseDepMap[requiredModulePath][resource.path] = true;
}
}
}
var reverseDeps = this._reverseDependencyMap[modulePath];
return reverseDeps ? Object.keys(reverseDeps) : [];
};
Loader.prototype.requireMock = function(currPath, moduleName) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
if (this._explicitlySetMocks.hasOwnProperty(moduleID)) {
return this._explicitlySetMocks[moduleID];
}
var manualMockResource = this._getResource('JSMock', moduleName);
var modulePath;
if (manualMockResource) {
modulePath = manualMockResource.path;
} else {
modulePath = this._moduleNameToPath(currPath, moduleName);
var moduleDir = path.dirname(modulePath);
var moduleFileName = path.basename(modulePath);
var potentialManualMock = path.join(moduleDir, '__mocks__', moduleFileName);
if (fs.existsSync(potentialManualMock)) {
manualMockResource = true;
modulePath = potentialManualMock;
}
}
if (this._mockRegistry.hasOwnProperty(modulePath)) {
return this._mockRegistry[modulePath];
}
if (manualMockResource) {
var moduleObj = {
exports: {},
__filename: modulePath
};
this._execModule(moduleObj);
this._mockRegistry[modulePath] = moduleObj.exports;
} else {
this._mockRegistry[modulePath] = this._generateMock(
currPath,
moduleName
);
}
return this._mockRegistry[modulePath];
};
Loader.prototype.requireModule = function(currPath, moduleName,
bypassRegistryCache) {
var modulePath;
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
var manualMockResource = null;
var moduleResource = null;
moduleResource = this._getResource('JS', moduleName);
manualMockResource = this._getResource('JSMock', moduleName);
if (!moduleResource
&& manualMockResource
&& manualMockResource.path !== this._isCurrentlyExecutingManualMock
&& this._explicitShouldMock[moduleID] !== false) {
modulePath = manualMockResource.path;
}
if (!modulePath) {
modulePath = this._moduleNameToPath(currPath, moduleName);
}
if (NODE_CORE_MODULES[moduleName]) {
return require(moduleName);
}
if (modulePath.indexOf(VENDOR_PATH) === 0) {
return require(modulePath);
}
if (!modulePath) {
throw new Error(
'Cannot find module \'' + moduleName + '\' from \'' + currPath +
'\''
);
}
var moduleObj;
if (modulePath && this._builtInModules.hasOwnProperty(modulePath)) {
moduleObj = this._builtInModules[modulePath](currPath);
}
if (!moduleObj && !bypassRegistryCache) {
moduleObj = this._moduleRegistry[modulePath];
}
if (!moduleObj) {
moduleObj = {
__filename: modulePath,
exports: {}
};
if (!bypassRegistryCache) {
this._moduleRegistry[modulePath] = moduleObj;
}
if (path.extname(modulePath) === '.json') {
moduleObj.exports = this._environment.global.JSON.parse(fs.readFileSync(
modulePath,
'utf8'
));
} else if(path.extname(modulePath) === '.node') {
moduleObj.exports = require(modulePath);
} else {
this._execModule(moduleObj);
}
}
return moduleObj.exports;
};
Loader.prototype.requireModuleOrMock = function(currPath, moduleName) {
if (this._shouldMock(currPath, moduleName)) {
return this.requireMock(currPath, moduleName);
} else {
return this.requireModule(currPath, moduleName);
}
};
Loader.prototype.getJestRuntime = function(dir) {
return this._builtInModules['jest-runtime'](dir).exports;
};
Loader.prototype.resetModuleRegistry = function() {
this._mockRegistry = {};
this._moduleRegistry = {};
this._builtInModules = {
'jest-runtime': function(currPath) {
var jestRuntime = {
exports: {
addMatchers: function(matchers) {
var jasmine = this._environment.global.jasmine;
var spec = jasmine.getEnv().currentSpec;
spec.addMatchers(matchers);
}.bind(this),
autoMockOff: function() {
this._shouldAutoMock = false;
return jestRuntime.exports;
}.bind(this),
autoMockOn: function() {
this._shouldAutoMock = true;
return jestRuntime.exports;
}.bind(this),
clearAllTimers: function() {
this._environment.fakeTimers.clearAllTimers();
}.bind(this),
dontMock: function(moduleName) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
this._explicitShouldMock[moduleID] = false;
return jestRuntime.exports;
}.bind(this),
getTestEnvData: function() {
var frozenCopy = {};
Object.keys(this._config.testEnvData).forEach(function(key) {
frozenCopy[key] = this._config.testEnvData[key];
}, this);
Object.freeze(frozenCopy);
return frozenCopy;
}.bind(this),
genMockFromModule: function(moduleName) {
return this._generateMock(
this._currentlyExecutingModulePath,
moduleName
);
}.bind(this),
genMockFunction: function() {
return moduleMocker.getMockFunction();
},
mock: function(moduleName) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
this._explicitShouldMock[moduleID] = true;
return jestRuntime.exports;
}.bind(this),
resetModuleRegistry: function() {
var globalMock;
for (var key in this._environment.global) {
globalMock = this._environment.global[key];
if ((typeof globalMock === 'object' && globalMock !== null)
|| typeof globalMock === 'function') {
globalMock._isMockFunction && globalMock.mockClear();
}
}
if (this._environment.global.mockClearTimers) {
this._environment.global.mockClearTimers();
}
this.resetModuleRegistry();
return jestRuntime.exports;
}.bind(this),
runAllTicks: function() {
this._environment.fakeTimers.runAllTicks();
}.bind(this),
runAllTimers: function() {
this._environment.fakeTimers.runAllTimers();
}.bind(this),
runOnlyPendingTimers: function() {
this._environment.fakeTimers.runOnlyPendingTimers();
}.bind(this),
setMock: function(moduleName, moduleExports) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
this._explicitShouldMock[moduleID] = true;
this._explicitlySetMocks[moduleID] = moduleExports;
return jestRuntime.exports;
}.bind(this),
useFakeTimers: function() {
this._environment.fakeTimers.useFakeTimers();
}.bind(this),
useRealTimers: function() {
this._environment.fakeTimers.useRealTimers();
}.bind(this)
}
};
jestRuntime.exports.genMockFn = jestRuntime.exports.genMockFunction;
return jestRuntime;
}.bind(this),
'node-haste': function() {
return {
exports: {
getResourceMap: function() {
return this._resourceMap;
}.bind(this)
}
};
}.bind(this),
'mocks': function(currPath) {
var mocks = {
exports: {
generateFromMetadata: moduleMocker.generateFromMetadata,
getMetadata: moduleMocker.getMetadata,
getMockFunction: function() {
return this.requireModule(
currPath,
'jest-runtime'
).genMockFn();
}.bind(this),
}
};
mocks.exports.getMockFn = mocks.exports.getMockFunction;
return mocks;
}.bind(this),
'mock-modules': function(currPath) {
var mockModules = {
exports: {
dontMock: function(moduleName) {
this.requireModule(
currPath,
'jest-runtime'
).dontMock(moduleName);
return mockModules.exports;
}.bind(this),
mock: function(moduleName) {
this.requireModule(
currPath,
'jest-runtime'
).mock(moduleName);
return mockModules.exports;
}.bind(this),
autoMockOff: function() {
this.requireModule(
currPath,
'jest-runtime'
).autoMockOff();
return mockModules.exports;
}.bind(this),
autoMockOn: function() {
this.requireModule(
currPath,
'jest-runtime'
).autoMockOn();
return mockModules.exports;
}.bind(this),
dumpCache: function() {
this.requireModule(
currPath,
'jest-runtime'
).resetModuleRegistry();
return mockModules.exports;
}.bind(this),
setMock: function(moduleName, moduleExports) {
this.requireModule(
currPath,
'jest-runtime'
).setMock(moduleName, moduleExports);
return mockModules.exports;
}.bind(this),
hasDependency: function(moduleAName, moduleBName) {
var traversedModules = {};
var self = this;
function _recurse(moduleAName, moduleBName) {
traversedModules[moduleAName] = true;
if (moduleAName === moduleBName) {
return true;
}
var moduleAResource = self._getResource('JS', moduleAName);
return !!(
moduleAResource
&& moduleAResource.requiredModules
&& moduleAResource.requiredModules.some(function(dep) {
return !traversedModules[dep] && _recurse(dep, moduleBName);
})
);
}
return _recurse(moduleAName, moduleBName);
}.bind(this),
generateMock: function(moduleName) {
return this.requireModule(
currPath,
'jest-runtime'
).genMockFromModule(moduleName);
}.bind(this),
useActualTimers: function() {
this.requireModule(
currPath,
'jest-runtime'
).useActualTimers();
}.bind(this),
loadActualModule: function(moduleName) {
return this.requireModule(
this._currentlyExecutingModulePath,
moduleName,
true
);
}.bind(this)
}
};
return mockModules;
}.bind(this)
};
};
module.exports = Loader;