Skip to content
Closed
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
40 changes: 22 additions & 18 deletions src/Runtime/Runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ const mockParentModule = {
exports: {},
};

const mockMetaDataCache = Object.create(null);
const moduleNameCache = Object.create(null);
const modulePathCache = Object.create(null);
const normalizedIDCache = Object.create(null);
const shouldMockModuleCache = Object.create(null);
const shouldUnmockTransitiveDependenciesCache = Object.create(null);
const transitiveShouldMock = Object.create(null);

const unmockCacheInitialized = new WeakMap();
const unmockRegExpCache = new WeakMap();

Expand Down Expand Up @@ -79,7 +76,7 @@ class Runtime {
const unmockPath = filePath => {
if (filePath && filePath.includes(constants.NODE_MODULES)) {
const moduleID = this._getNormalizedModuleID(filePath);
transitiveShouldMock[moduleID] = false;
this._transitiveShouldMock[moduleID] = false;
}
};

Expand All @@ -88,6 +85,11 @@ class Runtime {
unmockCacheInitialized.set(config, true);
}

this._mockMetaDataCache = Object.create(null);
this._shouldMockModuleCache = Object.create(null);
this._shouldUnmockTransitiveDependenciesCache = Object.create(null);
this._transitiveShouldMock = Object.create(null);

// Workers communicate the config as JSON so we have to create a regex
// object in the module loader instance.
this._mappedModuleNames = Object.create(null);
Expand Down Expand Up @@ -371,10 +373,10 @@ class Runtime {
_generateMock(currPath, moduleName) {
const modulePath = this._resolveModuleName(currPath, moduleName);

if (!(modulePath in mockMetaDataCache)) {
if (!(modulePath in this._mockMetaDataCache)) {
// This allows us to handle circular dependencies while generating an
// automock
mockMetaDataCache[modulePath] = moduleMocker.getMetadata({});
this._mockMetaDataCache[modulePath] = moduleMocker.getMetadata({});

// In order to avoid it being possible for automocking to potentially
// cause side-effects within the module environment, we need to execute
Expand Down Expand Up @@ -402,9 +404,11 @@ class Runtime {
`See: http://facebook.github.io/jest/docs/manual-mocks.html#content`
);
}
mockMetaDataCache[modulePath] = mockMetadata;
this._mockMetaDataCache[modulePath] = mockMetadata;
}
return moduleMocker.generateFromMetadata(mockMetaDataCache[modulePath]);
return moduleMocker.generateFromMetadata(
this._mockMetaDataCache[modulePath]
);
}

_resolveModuleName(currPath, moduleName) {
Expand Down Expand Up @@ -542,13 +546,13 @@ class Runtime {
if (
!this._shouldAutoMock ||
resolve.isCore(moduleName) ||
shouldUnmockTransitiveDependenciesCache[key]
this._shouldUnmockTransitiveDependenciesCache[key]
) {
return false;
}

if (moduleName in shouldMockModuleCache) {
return shouldMockModuleCache[moduleName];
if (moduleName in this._shouldMockModuleCache) {
return this._shouldMockModuleCache[moduleName];
}

const manualMockResource = this._getMockModule(moduleName);
Expand All @@ -557,14 +561,14 @@ class Runtime {
modulePath = this._resolveModuleName(currPath, moduleName);
} catch (e) {
if (manualMockResource) {
shouldMockModuleCache[moduleName] = true;
this._shouldMockModuleCache[moduleName] = true;
return true;
}
throw e;
}

if (this._unmockList && this._unmockList.test(modulePath)) {
shouldMockModuleCache[moduleName] = false;
this._shouldMockModuleCache[moduleName] = false;
return false;
}

Expand All @@ -576,15 +580,15 @@ class Runtime {
(
(this._unmockList && this._unmockList.test(currPath)) ||
explicitShouldMock[currentModuleID] === false ||
transitiveShouldMock[currentModuleID] === false
this._transitiveShouldMock[currentModuleID] === false
)
) {
transitiveShouldMock[moduleID] = false;
shouldUnmockTransitiveDependenciesCache[key] = true;
this._transitiveShouldMock[moduleID] = false;
this._shouldUnmockTransitiveDependenciesCache[key] = true;
return false;
}

return shouldMockModuleCache[moduleName] = true;
return this._shouldMockModuleCache[moduleName] = true;
}

_resolveStubModuleName(moduleName) {
Expand Down