denoland/deno · error · AssertionError

Missing snapshot file.

Error message

Missing snapshot file.

What it means

AssertionError from assertSnapshot() (cli/js/40_test_snapshot.js): the test asserts a snapshot but the snapshot file (context.fileExists is false) does not exist, and the run is not in update mode. Snapshot files are only created/updated when `deno test --update-snapshots` runs, so a first run without the flag cannot pass.

Source

Thrown at cli/js/40_test_snapshot.js:353

  const actualSnapshot = serializer(actual);
  if (typeof actualSnapshot !== "string") {
    throw new TypeError("Snapshot serializer must return a string");
  }

  const expectedSnapshot = MapPrototypeGet(context.currentValues, name);

  if (getIsUpdateMode()) {
    if (actualSnapshot !== expectedSnapshot) {
      MapPrototypeSet(context.updatedValues, name, actualSnapshot);
      if (!ArrayPrototypeIncludes(context.updatedNames, name)) {
        ArrayPrototypePush(context.updatedNames, name);
      }
    }
    return;
  }

  if (!context.fileExists) {
    throw new AssertionError(
      getErrorMessage("Missing snapshot file.", options),
    );
  }
  if (expectedSnapshot === undefined) {
    throw new AssertionError(
      getErrorMessage(`Missing snapshot: ${name}`, options),
    );
  }
  if (actualSnapshot === expectedSnapshot) {
    return;
  }
  throw new AssertionError(
    getErrorMessage(
      getSnapshotNotMatchMessage(actualSnapshot, expectedSnapshot),
      options,
    ),
  );
}

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Run `deno test --update-snapshots` once to create the file, then commit it
  2. Ensure the __snapshot__ directory is not gitignored and ships with the repo
  3. In CI, run the plain `deno test` (no flag) so committed snapshots are verified rather than rewritten

Example fix

# before
deno test                     # AssertionError: Missing snapshot file.

# after
deno test --update-snapshots   # creates/refreshes __snapshot__ files; commit them
git add **/__snapshot__/** && git commit -m "test: add snapshots"
Defensive patterns

Strategy: validation

Validate before calling

// before running the suite, verify the snapshot file exists
import { existsSync } from "node:fs";
if (!existsSync(new URL("./__snapshot__/<file>_test.ts.snap", import.meta.url))) {
  // first run: generate instead of fail
  await new Deno.Command(Deno.execPath(), {
    args: ["test", "--update-snapshots", import.meta.filename],
  }).spawn().status;
}

Prevention

When it happens

Trigger: Adding t.assertSnapshot() to a test file whose __snapshot__ file hasn't been generated yet; deleting or renaming the snapshot file; running tests in a fresh checkout/CI where snapshot files are not committed; CI running a path-filtered subset before snapshots were ever committed.

Common situations: First introduction of snapshot testing to a repo without immediately running with --update-snapshots; .gitignore accidentally excluding __snapshot__ directories; snapshot file generated locally but not committed, so CI fails.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/16c602ab9f6457bf. Report an issue: GitHub.