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

Method tryLoadGraphSync

packages/jest-runtime/src/internals/EsmLoader.ts:331–592  ·  view source on GitHub ↗
(
    rootPath: string,
    rootQuery: string,
    mode: SyncEsmMode,
  )

Source from the content-addressed store, hash-verified

329 // through `requireEsmModule` (sync require entry) or via `loadEsmModule`
330 // (the legacy async entry, which first-tries this).
331 tryLoadGraphSync(
332 rootPath: string,
333 rootQuery: string,
334 mode: SyncEsmMode,
335 ): ESModule | null {
336 if (
337 this.testState.bailIfTornDown(
338 'You are trying to `import` a file after the Jest environment has been torn down.',
339 )
340 ) {
341 return null;
342 }
343
344 const registry = this.registries.getActiveEsmRegistry();
345 const rootKey = rootPath + rootQuery;
346
347 const cached = registry.get(rootKey);
348 if (cached) {
349 if (cached instanceof Promise) return null;
350 // The legacy `loadEsmModule` source-text branch does `registry.set`
351 // while the `SourceTextModule` is still `'unlinked'` (link runs later
352 // in `linkAndEvaluateModule`); accessing `.namespace` on a non-evaluated
353 // module throws `ERR_VM_MODULE_STATUS`. Surface settled entries
354 // (`'evaluated'` / `'errored'`); bail otherwise.
355 if (cached.status === 'evaluated') return cached as ESModule;
356 if (cached.status === 'errored') throw cached.error;
357 return null;
358 }
359
360 const context = this.getContext();
361
362 if (this.transformCache.hasMutex(rootKey)) return null;
363
364 const scratch = new Map<string, ScratchEntry>();
365 const worklist: Array<WorklistEntry> = [
366 {cacheKey: rootKey, modulePath: rootPath},
367 ];
368
369 while (worklist.length > 0) {
370 const {cacheKey, modulePath} = worklist.pop()!;
371 if (scratch.has(cacheKey)) continue;
372
373 // Registry first, mutex second. Same settled-status gate as the root -
374 // anything in `'unlinked'` / `'linking'` / `'linked'` / `'evaluating'`
375 // is the legacy path mid-flight on this dep. Plugging an unlinked
376 // module into the parent's `linkRequests` would fail Node's link
377 // cascade; plugging a `'linked'` one would skip its body. Bail.
378 const fromRegistry = registry.get(cacheKey);
379 if (fromRegistry instanceof Promise) return null;
380 if (fromRegistry) {
381 if (fromRegistry.status === 'errored') throw fromRegistry.error;
382 if (fromRegistry.status !== 'evaluated') return null;
383 scratch.set(cacheKey, {
384 cacheKey,
385 kind: 'synthetic',
386 module: fromRegistry,
387 });
388 continue;

Callers 3

requireEsmModuleMethod · 0.95
loadEsmModuleMethod · 0.95
EsmLoader.test.tsFile · 0.80

Calls 15

getContextMethod · 0.95
buildSyncDataUriEntryMethod · 0.95
buildSyncWasmEntryMethod · 0.95
buildCoreSyntheticModuleFunction · 0.90
isWasmFunction · 0.90
buildJsonSyntheticModuleFunction · 0.90
invariantFunction · 0.90
makeRequireAsyncErrorFunction · 0.85
validateImportAttributesFunction · 0.85
moduleHasAsyncGraphFunction · 0.85
bailIfTornDownMethod · 0.80

Tested by

no test coverage detected