in packages/jest-runtime/src/index.ts [531:666]
private async resolveModule<T = unknown>(
specifier: string,
referencingIdentifier: string,
context: VMContext,
): Promise<T> {
if (this.isTornDown) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
// @ts-expect-error - exiting
return;
}
if (specifier === '@jest/globals') {
const fromCache = this._esmoduleRegistry.get('@jest/globals');
if (fromCache) {
return fromCache;
}
const globals = this.getGlobalsForEsm(referencingIdentifier, context);
this._esmoduleRegistry.set('@jest/globals', globals);
return globals;
}
if (specifier.startsWith('data:')) {
if (
await this._shouldMockModule(
referencingIdentifier,
specifier,
this._explicitShouldMockModule,
{conditions: this.esmConditions},
)
) {
return this.importMock(referencingIdentifier, specifier, context);
}
const fromCache = this._esmoduleRegistry.get(specifier);
if (fromCache) {
return fromCache;
}
const match = specifier.match(dataURIRegex);
if (!match || !match.groups) {
throw new Error('Invalid data URI');
}
const mime = match.groups.mime;
if (mime === 'application/wasm') {
throw new Error('WASM is currently not supported');
}
const encoding = match.groups.encoding;
let code = match.groups.code;
if (!encoding || encoding === 'charset=utf-8') {
code = decodeURIComponent(code);
} else if (encoding === 'base64') {
code = Buffer.from(code, 'base64').toString();
} else {
throw new Error(`Invalid data URI encoding: ${encoding}`);
}
let module;
if (mime === 'application/json') {
module = new SyntheticModule(
['default'],
function () {
const obj = JSON.parse(code);
// @ts-expect-error: TS doesn't know what `this` is
this.setExport('default', obj);
},
{context, identifier: specifier},
);
} else {
module = new SourceTextModule(code, {
context,
identifier: specifier,
importModuleDynamically: async (
specifier: string,
referencingModule: VMModule,
) => {
invariant(
runtimeSupportsVmModules,
'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules',
);
const module = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context,
);
return this.linkAndEvaluateModule(module);
},
initializeImportMeta(meta: ImportMeta) {
meta.url = specifier;
},
});
}
this._esmoduleRegistry.set(specifier, module);
return module;
}
if (specifier.startsWith('file://')) {
specifier = fileURLToPath(specifier);
}
const [path, query] = specifier.split('?');
if (
await this._shouldMockModule(
referencingIdentifier,
path,
this._explicitShouldMockModule,
{conditions: this.esmConditions},
)
) {
return this.importMock(referencingIdentifier, path, context);
}
const resolved = await this._resolveModule(referencingIdentifier, path, {
conditions: this.esmConditions,
});
if (
this._resolver.isCoreModule(resolved) ||
this.unstable_shouldLoadAsEsm(resolved)
) {
return this.loadEsmModule(resolved, query);
}
return this.loadCjsAsEsm(referencingIdentifier, resolved, context);
}