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

Method resolveModule

packages/jest-runtime/src/internals/EsmLoader.ts:1086–1185  ·  view source on GitHub ↗
(
    specifier: string,
    referencingIdentifier: string,
    context: VMContext,
  )

Source from the content-addressed store, hash-verified

1084 }
1085
1086 private async resolveModule<T = unknown>(
1087 specifier: string,
1088 referencingIdentifier: string,
1089 context: VMContext,
1090 ): Promise<T> {
1091 if (
1092 this.testState.bailIfTornDown(
1093 'You are trying to `import` a file after the Jest environment has been torn down.',
1094 )
1095 ) {
1096 // @ts-expect-error -- exiting
1097 return;
1098 }
1099
1100 const registry = this.registries.getActiveEsmRegistry();
1101
1102 if (specifier === '@jest/globals') {
1103 const globalsIdentifier = `@jest/globals/${referencingIdentifier}`;
1104 const fromCache = registry.get(globalsIdentifier);
1105 if (fromCache) {
1106 return fromCache as T;
1107 }
1108 const globals = evaluateSyntheticModule(
1109 this.jestGlobals.esmGlobalsModule(referencingIdentifier, context),
1110 );
1111 registry.set(globalsIdentifier, globals);
1112 return globals as T;
1113 }
1114
1115 if (specifier.startsWith('data:')) {
1116 const dataDecision = await this.mockState.shouldMockEsmAsync(
1117 referencingIdentifier,
1118 specifier,
1119 );
1120 if (dataDecision.shouldMock) {
1121 return this.importMock(specifier, dataDecision.moduleID, context);
1122 }
1123 const fromCache = registry.get(specifier);
1124 if (fromCache) {
1125 return fromCache as T;
1126 }
1127 const {mime, code} = parseDataUri(specifier);
1128 let module: VMModule;
1129 if (mime === 'application/wasm') {
1130 module = await this.importWasmModule(
1131 new Uint8Array(code as Buffer),
1132 specifier,
1133 context,
1134 );
1135 } else if (mime === 'application/json') {
1136 module = buildJsonSyntheticModule(code as string, specifier, context);
1137 } else {
1138 module = new SourceTextModule(code as string, {
1139 context,
1140 identifier: specifier,
1141 importModuleDynamically: this.dynamicImport,
1142 initializeImportMeta(meta) {
1143 meta.url = specifier;

Callers 2

dynamicImportFromCjsMethod · 0.95
resolveCachedMethod · 0.45

Calls 15

importMockMethod · 0.95
importWasmModuleMethod · 0.95
loadEsmModuleMethod · 0.95
loadCjsAsEsmMethod · 0.95
evaluateSyntheticModuleFunction · 0.90
buildJsonSyntheticModuleFunction · 0.90
parseDataUriFunction · 0.85
bailIfTornDownMethod · 0.80
getActiveEsmRegistryMethod · 0.80
esmGlobalsModuleMethod · 0.80
shouldMockEsmAsyncMethod · 0.80
resolveEsmAsyncMethod · 0.80

Tested by

no test coverage detected