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

Method loadCjsAsEsm

packages/jest-runtime/src/internals/EsmLoader.ts:1272–1303  ·  view source on GitHub ↗
(
    from: string,
    modulePath: string,
    context: VMContext,
  )

Source from the content-addressed store, hash-verified

1270 }
1271
1272 private loadCjsAsEsm(
1273 from: string,
1274 modulePath: string,
1275 context: VMContext,
1276 ): SyntheticModule | Promise<VMModule> {
1277 const registry = this.registries.getActiveEsmRegistry();
1278 const cached = registry.get(modulePath);
1279 if (cached) {
1280 return cached as SyntheticModule | Promise<VMModule>;
1281 }
1282
1283 let synthetic: SyntheticModule;
1284 try {
1285 synthetic = this.buildCjsAsEsmSyntheticModule(from, modulePath, context);
1286 } catch (error) {
1287 if (!isCjsParseError(error)) {
1288 throw error;
1289 }
1290 // The file may contain ESM syntax with no ESM marker (.mjs /
1291 // "type":"module") - try loading as native ESM. If the ESM parser also
1292 // rejects it, the original CJS error was the genuine one.
1293 return this.loadEsmModule(modulePath).catch(esmError => {
1294 throw isError(esmError) && esmError.name === 'SyntaxError'
1295 ? error
1296 : esmError;
1297 });
1298 }
1299
1300 const evaluated = evaluateSyntheticModule(synthetic);
1301 registry.set(modulePath, evaluated);
1302 return evaluated;
1303 }
1304
1305 private async importMock<T = unknown>(
1306 moduleName: string,

Callers 1

resolveModuleMethod · 0.95

Calls 9

loadEsmModuleMethod · 0.95
isCjsParseErrorFunction · 0.90
evaluateSyntheticModuleFunction · 0.90
getActiveEsmRegistryMethod · 0.80
catchMethod · 0.80
isErrorFunction · 0.50
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected