avajs/ava · error · InvalidSnapshotError

Invalid snapshot file

Error message

Invalid snapshot file

What it means

This invalid-snapshot error means the snapshot file does not have the structure AVA expects: the reader could not find the newline byte (0x0A) that terminates the readable prefix header. Without that delimiter AVA cannot locate the version field, so the file is treated as corrupt and extractCompressedSnapshot() throws InvalidSnapshotError.

Source

Thrown at lib/snapshot-manager.js:236

	const sha256sum = crypto.createHash('sha256').update(compressed).digest();
	return Buffer.concat([
		READABLE_PREFIX,
		VERSION_HEADER,
		sha256sum,
		compressed,
	], READABLE_PREFIX.byteLength + VERSION_HEADER.byteLength + SHA_256_HASH_LENGTH + compressed.byteLength);
}

export function extractCompressedSnapshot(buffer, snapPath) {
	if (isLegacySnapshot(buffer)) {
		throw new LegacyError(snapPath);
	}

	// The version starts after the readable prefix, which is ended by a newline
	// byte (0x0A).
	const newline = buffer.indexOf(0x0A);
	if (newline === -1) {
		throw new InvalidSnapshotError(snapPath);
	}

	const versionOffset = newline + 1;
	const version = buffer.readUInt16LE(versionOffset);
	if (version !== VERSION) {
		throw new VersionMismatchError(snapPath, version);
	}

	const sha256sumOffset = versionOffset + 2;
	const compressedOffset = sha256sumOffset + SHA_256_HASH_LENGTH;
	const compressed = buffer.slice(compressedOffset);

	return {
		version, compressed, sha256sumOffset, compressedOffset,
	};
}

function decodeSnapshots(buffer, snapPath) {

View on GitHub (pinned to bbfd946322)

Solutions

  1. Regenerate the file: delete the corrupt .snap and rerun AVA with --update-snapshots
  2. Check gitattributes/line-ending config so .snap files are not altered (add '*. -text' or mark snapshots binary) and restore the original via git checkout -- <file>.snap
  3. Restore the file from version control or a backup if it was truncated or hand-edited

Example fix

// before (.gitattributes missing)
// after (.gitattributes)
*.snap -text
*.snap binary
Defensive patterns

Strategy: validation

Validate before calling

const buf = fs.readFileSync(snapPath);
if (buf.indexOf(0x0A) === -1) {
  throw new Error(`${snapPath} has no header newline — corrupt or wrong format; regenerate with --update-snapshots`);
}

Type guard

function looksLikeAvaSnapshot(buffer) {
  return Buffer.isBuffer(buffer) && buffer.indexOf(0x0A) !== -1;
}

Try / catch

try {
  decodeSnapshots(buffer, snapPath);
} catch (err) {
  if (/Invalid snapshot file/i.test(err.message)) {
    // restore from git or regenerate with --update-snapshots
  } else { throw err; }
}

Prevention

When it happens

Trigger: Calling extractCompressedSnapshot/decodeSnapshots on a buffer with no 0x0A byte in the header region — e.g. a truncated, hand-edited, binary-corrupted, or entirely foreign .snap file (line-ending conversion that removed the newline, git filters, or partial file writes).

Common situations: Git autocrlf or other line-ending filters mangling committed snapshot files; disk truncation; someone editing the .snap file by hand; opening a snapshot file produced by a different tool.

Related errors


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