in packages/jest-runtime/src/index.ts [1847:1919]
private async _shouldMockModule(
from: string,
moduleName: string,
explicitShouldMock: Map<string, boolean>,
options: ResolveModuleConfig,
): Promise<boolean> {
const moduleID = await this._resolver.getModuleIDAsync(
this._virtualMocks,
from,
moduleName,
options,
);
const key = from + path.delimiter + moduleID;
if (explicitShouldMock.has(moduleID)) {
// guaranteed by `has` above
return explicitShouldMock.get(moduleID)!;
}
if (
!this._shouldAutoMock ||
this._resolver.isCoreModule(moduleName) ||
this._shouldUnmockTransitiveDependenciesCache.get(key)
) {
return false;
}
if (this._shouldMockModuleCache.has(moduleID)) {
// guaranteed by `has` above
return this._shouldMockModuleCache.get(moduleID)!;
}
let modulePath;
try {
modulePath = await this._resolveModule(from, moduleName, options);
} catch (e) {
const manualMock = await this._resolver.getMockModuleAsync(
from,
moduleName,
);
if (manualMock) {
this._shouldMockModuleCache.set(moduleID, true);
return true;
}
throw e;
}
if (this._unmockList && this._unmockList.test(modulePath)) {
this._shouldMockModuleCache.set(moduleID, false);
return false;
}
// transitive unmocking for package managers that store flat packages (npm3)
const currentModuleID = await this._resolver.getModuleIDAsync(
this._virtualMocks,
from,
undefined,
options,
);
if (
this._transitiveShouldMock.get(currentModuleID) === false ||
(from.includes(NODE_MODULES) &&
modulePath.includes(NODE_MODULES) &&
((this._unmockList && this._unmockList.test(from)) ||
explicitShouldMock.get(currentModuleID) === false))
) {
this._transitiveShouldMock.set(moduleID, false);
this._shouldUnmockTransitiveDependenciesCache.set(key, true);
return false;
}
this._shouldMockModuleCache.set(moduleID, true);
return true;
}