denoland/deno · error · AssertionError
Missing snapshot file.
Error message
Missing snapshot file.
What it means
In check mode (a normal `deno test` run), assertSnapshot reads the snapshot file resolved from options.dir/options.path (defaulting next to the test file). If that file does not exist at all, it throws AssertionError "Missing snapshot file." before any comparison (cli/js/40_test_snapshot.js). In update mode (`deno test --update`) the same situation instead creates the file, which is why this error classically appears on the first check-mode run of brand-new snapshot tests.
Solutions
- Run `deno test --update` (or `-u`) once to generate the snapshot file, then re-run plain `deno test`.
- Commit the generated snapshot file to version control so CI and teammates have it.
- Verify options.dir/options.path resolve to the same location in generate and check runs.
- Check that .gitignore does not exclude the __snapshots__ directory or *.snap files.
Example fix
# before deno test # AssertionError: Missing snapshot file. # after deno test --update # create/refresh snapshots deno test # now passes
Defensive patterns
Strategy: validation
Validate before calling
// Fail with a clear message before tests run if the snapshot file is absent:
import { assert } from "jsr:@std/assert";
const snap = new URL("./__snapshots__/main.spec.snap", import.meta.url);
if (import.meta.main === false && !isFirstRun) {
assert(Deno.statSync(snap).isFile, `missing snapshot file: ${snap.pathname} — run 'deno test --update'`);
} Try / catch
Deno.test("snap", async (t) => {
try {
await t.assertSnapshot(value);
} catch (err) {
if (err instanceof Deno.errors.NotFound || String(err.message).includes("Missing snapshot file")) {
throw new Error("No snapshot file — run 'deno test --update' first");
}
throw err;
}
}); Prevention
- Run `deno test --update` once when adding new snapshot tests, then run plain `deno test`.
- Commit snapshot files to version control and make sure .gitignore excludes neither __snapshots__/ nor *.snap.
- Keep options.dir/options.path identical between the run that generates and the run that checks.
When it happens
Trigger: Running `deno test` (check mode) on tests that call assertSnapshot for the first time with no snapshot file present; the __snapshots__ file was deleted or never committed; options.dir/options.path point at a directory where no snapshot file exists.
Common situations: New snapshot tests written but never run with --update; the snapshot file missing from version control so CI fails locally-passing tests; a custom dir/path option that does not match where snapshots were generated; .gitignore rules swallowing the snapshots directory.
Related errors
- Expected the second argument to assertSnapshot() to be an…
- Missing snapshot
- Snapshot does not match:\n\n
- Snapshot serializer must return a string
- A file exists at the destination
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/16c602ab9f6457bf.
Report an issue: GitHub.
Appendix: 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 9ad36f7a2c)