jestjs/jest · error · Error

Invalid data URI encoding: ${encoding}

Error message

Invalid data URI encoding: ${encoding}

What it means

Thrown by parseDataUri when a WASM data URI declares an encoding that is not `base64`. Since wasm is binary, the only valid encoding is base64 — anything else (e.g. `charset=utf-8`) is rejected at parse time.

Source

Thrown at packages/jest-runtime/src/internals/EsmLoader.ts:142

// 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>();
// Soft cap so a long-lived process (watch mode, --runInBand) can't grow the
// set without bound. When we hit it we drop everything; users see at most one

View on GitHub (pinned to f49721c78e)

Solutions

  1. Change the encoding to `;base64`: `data:application/wasm;base64,<base64>`.
  2. Regenerate the URI with `Buffer.from(bytes).toString('base64')`.

Example fix

// before
import wasm from 'data:application/wasm;charset=utf-8,<text>';

// after
import wasm from 'data:application/wasm;base64,<base64>';
Defensive patterns

Strategy: validation

Validate before calling

function assertWasmEncoding(uri) {
  const m = uri.match(/^data:application\/wasm;([^,]+),/);
  if (!m) throw new Error('not a wasm data URI');
  if (m[1] !== 'base64') throw new Error(`wasm data URI must use base64, got ${m[1]}`);
}

Type guard

function usesBase64WasmEncoding(s: string): boolean {
  return /^data:application\/wasm;base64,/.test(s);
}

Prevention

When it happens

Trigger: Importing `data:application/wasm;charset=utf-8,...` or any encoding token other than `base64` for a wasm URI.

Common situations: Reusing a JS data URI template (`;charset=utf-8`) for wasm; a bundler plugin emitting the wrong encoding for wasm modules.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/6f8e9658aa267aca.json. Report an issue: GitHub.