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

Method loadEsmModule

packages/jest-runtime/src/internals/EsmLoader.ts:962–1084  ·  view source on GitHub ↗
(
    modulePath: string,
    query = '',
  )

Source from the content-addressed store, hash-verified

960 }
961
962 private async loadEsmModule(
963 modulePath: string,
964 query = '',
965 ): Promise<ESModule> {
966 // Two gates here. `supportsSyncEvaluate` is a Node-version check: the
967 // sync core relies on `SyntheticModule` starting `'linked'` and on
968 // `evaluate()` completing sync, both of which need v22.21+ / v24.8+.
969 // `canResolveSync` is a configured-resolver check: with an async-only
970 // user resolver `findNodeModule` silently falls back to the default
971 // resolver and would silently miss user mappings.
972 if (supportsSyncEvaluate && this.resolution.canResolveSync()) {
973 const synced = this.tryLoadGraphSync(modulePath, query, 'sync-preferred');
974 if (synced) return synced;
975 }
976
977 const cacheKey = modulePath + query;
978 const registry = this.registries.getActiveEsmRegistry();
979
980 if (this.transformCache.hasMutex(cacheKey)) {
981 await this.transformCache.awaitMutex(cacheKey);
982 }
983
984 if (!registry.has(cacheKey)) {
985 const context = this.getContext();
986
987 let transformResolve: () => void;
988 let transformReject: (error?: unknown) => void;
989
990 this.transformCache.setMutex(
991 cacheKey,
992 new Promise((resolve, reject) => {
993 transformResolve = resolve;
994 transformReject = reject;
995 }),
996 );
997
998 invariant(
999 transformResolve! && transformReject!,
1000 'Promise initialization should be sync - please report this bug to Jest!',
1001 );
1002
1003 try {
1004 if (isWasm(modulePath)) {
1005 const wasm = this.importWasmModule(
1006 this.fileCache.readFileBuffer(modulePath),
1007 modulePath,
1008 context,
1009 );
1010 registry.set(cacheKey, wasm);
1011 transformResolve();
1012 return wasm;
1013 }
1014
1015 if (this.resolution.isCoreModule(modulePath)) {
1016 const core = evaluateSyntheticModule(
1017 buildCoreSyntheticModule(modulePath, context, (name, prefix) =>
1018 this.coreModule.require(name, prefix),
1019 ),

Callers 3

loadAndEvaluateMethod · 0.95
resolveModuleMethod · 0.95
loadCjsAsEsmMethod · 0.95

Calls 15

tryLoadGraphSyncMethod · 0.95
getContextMethod · 0.95
importWasmModuleMethod · 0.95
invariantFunction · 0.90
isWasmFunction · 0.90
evaluateSyntheticModuleFunction · 0.90
buildCoreSyntheticModuleFunction · 0.90
buildJsonSyntheticModuleFunction · 0.90
getActiveEsmRegistryMethod · 0.80
hasMutexMethod · 0.80
awaitMutexMethod · 0.80
setMutexMethod · 0.80

Tested by

no test coverage detected