denoland/deno · error · AssertionError

Snapshot does not match:\n\n ${green("[Diff]")} ${green("

Error message

Snapshot does not match:\n\n    ${green("[Diff]")} ${green("Actual")} / ${red("Expected")}\n\n${diff}\n\nTo update snapshots, run\n    deno test --update-snapshots [files]...\n

What it means

Thrown by the built-in snapshot testing API (Deno.TestContext.assertSnapshot, implemented in cli/js/40_test_snapshot.js). The actual value serialized during the test no longer matches the string stored in the __snapshots__/*.snap file for that test and snapshot name. The message contains a colored diff plus the exact command needed to regenerate the snapshots.

Source

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

      }
    }
    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,
    ),
  );
}

function buildSnapshotFileContent(names, getValue) {
  const buf = ["export const snapshot = {};"];
  for (const name of new SafeArrayIterator(names)) {
    const value = getValue(name);
    if (value === undefined) {
      continue;
    }
    let formatted = escapeStringForJs(value);
    formatted = StringPrototypeIncludes(formatted, "\n")
      ? `\n${formatted}\n`
      : formatted;

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Review the diff in the failure output; if the new value is intended, run `deno test --update-snapshots <files>...` and commit the regenerated .snap file
  2. If the change is unintended, fix the code producing the value until it matches the stored snapshot
  3. For non-deterministic inputs, freeze or strip them before snapshotting (fixed dates, seeded RNG, path basenames)
  4. Give distinct names via assertSnapshot(t, value, { msg: 'stable-name' }) so edits and test reordering do not collide with counter-based names

Example fix

// before
Deno.test("greeting", async (t) => {
  await t.assertSnapshot(greet(new Date())); // Date differs every run
});

// after
Deno.test("greeting", async (t) => {
  await t.assertSnapshot(greet(new Date("2026-01-01T00:00:00Z"))); // frozen input
});
Defensive patterns

Strategy: validation

Validate before calling

// Keep snapshot inputs deterministic before asserting
function stabilize(value: unknown): unknown {
  return JSON.stringify(value, (_k, v) =>
    typeof v === "string" && v.includes(Deno.build.os) ? "<os-dependent>" : v,
  );
}

Prevention

When it happens

Trigger: A snapshot file exists and contains an entry for the snapshot's name (counter-based or set via options.msg), but the current serialized actual value (string or property-based snapshot) is not strictly equal to the stored snapshot string.

Common situations: Intentional output changes after a refactor; non-deterministic values captured in snapshots (Date.now(), Math.random(), temp dirs, absolute paths); CRLF vs LF differences across operating systems; tests reordered so counter-based snapshot names now map to different values.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/06933944324086f3. Report an issue: GitHub.