jestjs/jest · error · Error
Missing data URI encoding
Error message
Missing data URI encoding
What it means
Thrown by parseDataUri when the MIME type is `application/wasm` but no encoding segment is present. WASM data URIs must be base64-encoded (binary cannot live in a URL-decoded text body), so Jest refuses to parse one without an explicit `;base64`.
Source
Thrown at packages/jest-runtime/src/internals/EsmLoader.ts:140
return error;
}
// Decode a `data:` URI specifier into its mime type and decoded code/body.
// `application/wasm` returns a Buffer; everything else returns a UTF-8 string.
const dataURIRegex =
/^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;
function parseDataUri(specifier: string): {
mime: string;
code: string | Buffer;
} {
const match = specifier.match(dataURIRegex);
if (!match || !match.groups) {
throw new Error('Invalid data URI');
}
const {mime, encoding, code} = match.groups;
if (mime === 'application/wasm') {
if (!encoding) throw new Error('Missing data URI encoding');
if (encoding !== 'base64') {
throw new Error(`Invalid data URI encoding: ${encoding}`);
}
return {code: Buffer.from(code, 'base64'), mime};
}
if (!encoding || encoding === 'charset=utf-8') {
return {code: decodeURIComponent(code), mime};
}
if (encoding === 'base64') {
return {code: Buffer.from(code, 'base64').toString(), mime};
}
throw new Error(`Invalid data URI encoding: ${encoding}`);
}
// Mirrors Node's `validateAttributes` in lib/internal/modules/esm/assert.js.
// The only deliberate divergence: missing `type: 'json'` warns instead of
// throwing — see the JSON branch below.
const warnedMissingJsonAttributePairs = new Set<string>();View on GitHub (pinned to f49721c78e)
Solutions
- Add `;base64` to the URI: `data:application/wasm;base64,<base64-encoded-bytes>`.
- Generate the URI programmatically: `'data:application/wasm;base64,' + Buffer.from(wasmBytes).toString('base64')`.
Example fix
// before import wasm from 'data:application/wasm,AGFzbQ...'; // after import wasm from 'data:application/wasm;base64,AGFzbQ...';
Defensive patterns
Strategy: validation
Validate before calling
function buildWasmDataUri(bytes) {
if (!bytes || !bytes.length) throw new Error('empty wasm bytes');
return 'data:application/wasm;base64,' + Buffer.from(bytes).toString('base64');
} Type guard
function isBase64WasmDataUri(s: string): boolean {
return /^data:application\/wasm;base64,/.test(s);
} Prevention
- Always include `;base64` for wasm data URIs.
- Generate wasm URIs from a Buffer/Uint8Array via base64 encoding.
- Never hand-type binary content into a data URI.
When it happens
Trigger: Importing a `data:application/wasm,...` URI without the `;base64` encoding token. WASM bodies are binary and cannot be represented as URL-encoded text, so the encoding is mandatory.
Common situations: Hand-writing a WASM data URI and forgetting the encoding; copying a JS data URI pattern and swapping only the MIME to wasm; a code generator that omits the encoding for wasm modules.
Related errors
- Invalid data URI encoding: ${encoding}
- Invalid data URI
- jest.config.mts requires native TypeScript support. Ensure y
- Unable to load resolver at ${options.resolver}
- Attempting to import a mock without a factory
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/bd0b57076a6f91ef.json.
Report an issue: GitHub.