Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_generateMock(moduleId, ignoreManualMock) {
let module;
if (!this._mockMetaDataCache.hasOwnProperty(moduleId)) {
let origMockRegistry;
let origModuleRegistry;
// This allows us to handle circular dependencies while generating an
// automock.
this._mockMetaDataCache[moduleId] = moduleMocker.getMetadata({});
// In order to avoid it being possible for automocking to potentially cause
// side-effects within the module environment, we need to execute the module
// in isolation. This accomplishes that by temporarily clearing out the
// module and mock registries while the module being analyzed is executed.
//
// An example scenario where this could cause issue is if the module being
// mocked has calls into side-effectful APIs on another module.
origMockRegistry = this._mockRegistry;
origModuleRegistry = this._environment.global.installedModules;
this._mockRegistry = {};
this._environment.global.installedModules = {};
module = this._webpackRequireModule(moduleId);
// Restore the "real" module/mock registries
// module and mock registries while the module being analyzed is executed.
//
// An example scenario where this could cause issue is if the module being
// mocked has calls into side-effectful APIs on another module.
origMockRegistry = this._mockRegistry;
origModuleRegistry = this._environment.global.installedModules;
this._mockRegistry = {};
this._environment.global.installedModules = {};
module = this._webpackRequireModule(moduleId);
// Restore the "real" module/mock registries
this._mockRegistry = origMockRegistry;
this._environment.global.installedModules = origModuleRegistry;
this._mockMetaDataCache[moduleId] = moduleMocker.getMetadata(module);
}
// Check whether a manual mock was registered as a result of running the module
// via `jest._registerManualMock` / the `ManualMockLoader`.
if (!ignoreManualMock && this._manualMockMap.hasOwnProperty(moduleId)) {
return this._webpackRequireModule(this._manualMockMap[moduleId]);
}
return moduleMocker.generateFromMetadata(this._mockMetaDataCache[moduleId]);
}