MCPcopy
hub / github.com/jestjs/jest / _requireMockWithId

Method _requireMockWithId

packages/jest-runtime/src/index.ts:422–469  ·  view source on GitHub ↗
(
    from: string,
    moduleName: string,
    moduleID: string,
  )

Source from the content-addressed store, hash-verified

420 }
421
422 private _requireMockWithId<T>(
423 from: string,
424 moduleName: string,
425 moduleID: string,
426 ): T {
427 if (this.registries.hasMock(moduleID)) {
428 return this.registries.getMock(moduleID) as T;
429 }
430
431 const mockRegistry = this.registries.getActiveMockRegistry();
432
433 const factory = this.mockState.getCjsFactory(moduleID);
434 if (factory) {
435 const module = factory();
436 mockRegistry.set(moduleID, module);
437 return module as T;
438 }
439
440 const manualMockPath = this._resolution.findManualMock(from, moduleName);
441
442 if (manualMockPath) {
443 const localModule: InitialModule = {
444 children: [],
445 exports: {},
446 filename: manualMockPath,
447 id: manualMockPath,
448 isPreloading: false,
449 loaded: false,
450 path: path.dirname(manualMockPath),
451 };
452
453 this.cjsLoader.loadModule(
454 localModule,
455 from,
456 moduleName,
457 manualMockPath,
458 undefined,
459 mockRegistry as ModuleRegistry,
460 );
461
462 mockRegistry.set(moduleID, localModule.exports);
463 } else {
464 // Look for a real module to generate an automock from
465 mockRegistry.set(moduleID, this._generateMock(from, moduleName));
466 }
467
468 return mockRegistry.get(moduleID) as T;
469 }
470
471 private _getFullTransformationOptions(
472 options: TransformOptions = defaultTransformOptions,

Callers 2

requireMockMethod · 0.95
requireModuleOrMockMethod · 0.95

Calls 9

_generateMockMethod · 0.95
hasMockMethod · 0.80
getMockMethod · 0.80
getActiveMockRegistryMethod · 0.80
getCjsFactoryMethod · 0.80
findManualMockMethod · 0.80
loadModuleMethod · 0.80
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected