avajs/ava · error · AssertionError

Could not compare snapshot

Error message

Could not compare snapshot

What it means

AVA throws this AssertionError from t.snapshot() when the snapshot engine (compareWithSnapshot) raises a SnapshotError, meaning the snapshot file itself could not be read or compared — e.g. a corrupt snapshot file, a snapshot written by a different AVA version, or an unreadable/missing .snap file. The error name, snapshot path, and (for VersionMismatchError) the found/expected snapshot format versions are attached as improperUsage details so the user can diagnose the file-level problem.

Source

Thrown at lib/assert.js:664

					formattedDetails: [formatWithLabel('Called with:', message)],
				}));
			}

			let result;
			try {
				result = compareWithSnapshot({expected, message});
			} catch (error) {
				if (!(error instanceof SnapshotError)) {
					throw error;
				}

				const improperUsage = {assertion: 'snapshot', name: error.name, snapPath: error.snapPath};
				if (error instanceof VersionMismatchError) {
					improperUsage.snapVersion = error.snapVersion;
					improperUsage.expectedVersion = error.expectedVersion;
				}

				throw fail(new AssertionError(message ?? 'Could not compare snapshot', {
					asssertion: 't.snapshot()',
					improperUsage,
				}));
			}

			if (result.pass) {
				return pass();
			}

			if (result.actual) {
				throw fail(new AssertionError(message ?? 'Did not match snapshot', {
					assertion: 't.snapshot()',
					formattedDetails: [formatDescriptorDiff(result.actual, result.expected, {invert: true})],
				}));
			} else {
				// This can only occur in CI environments.
				throw fail(new AssertionError(message ?? 'No snapshot available — new snapshots are not created in CI environments', {
					assertion: 't.snapshot()',

View on GitHub (pinned to bbfd946322)

Solutions

  1. Regenerate snapshots: delete the stale test/snapshots directory or .snap files and re-run with AVA's update flag (npx ava --update-snapshots).
  2. Align the AVA version: if snapVersion != expectedVersion, upgrade (or, with care, downgrade) AVA so the snapshot format matches the existing files.
  3. Check that the snapshot path in improperUsage.snapPath exists, is readable by the test process, and has not been corrupted or hand-edited.
  4. Commit and sync .snap files across branches/CI so all environments use snapshots written by the same AVA version.

Example fix

// before: .snap file written by AVA 2.x run with AVA 4.x
// terminal
npx ava            // AssertionError: Could not compare snapshot

// after
rm -rf test/snapshots
npx ava --update-snapshots   // snapshots rewritten in current format, tests pass
Defensive patterns

Strategy: validation

Validate before calling

import fs from 'node:fs';
// Before running snapshot tests, ensure snapshot files exist, are readable, and use the current AVA's format.
const snapPath = 'test/snapshots/test.js.snap';
if (!fs.existsSync(snapPath) || !fs.accessSync(snapPath, fs.constants.R_OK)) {
  throw new Error(`Snapshot file missing/unreadable: ${snapPath} — run 'npx ava --update-snapshots' locally first`);
}

Prevention

When it happens

Trigger: Calling t.snapshot(value) when the underlying snapshot file at error.snapPath cannot be compared: the snapshot file was produced by an older/newer AVA with a different snapshot format version (VersionMismatchError with snapVersion/expectedVersion), or the snapshot engine threw another SnapshotError (e.g. unreadable or malformed snapshot file).

Common situations: Upgrading or downgrading AVA across snapshot-format versions while old testosterone-era .snap files remain; hand-editing or truncating snapshot files; CI caches restoring .snap files from a different AVA version; switching branches where .snap files were written by a different tool version; read-only filesystems causing snapshot errors.

Related errors


AI-assisted analysis of avajs/ava@bbfd946322 (2026-09-02). Data as JSON: /api/errors/e1409faf35e55668. Report an issue: GitHub.