{"record":{"id":"e1409faf35e55668","repo":"avajs/ava","slug":"could-not-compare-snapshot","errorCode":null,"errorMessage":"Could not compare snapshot","messagePattern":"Could not compare snapshot","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":664,"sourceCode":"\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', message)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tlet result;\n\t\t\ttry {\n\t\t\t\tresult = compareWithSnapshot({expected, message});\n\t\t\t} catch (error) {\n\t\t\t\tif (!(error instanceof SnapshotError)) {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\n\t\t\t\tconst improperUsage = {assertion: 'snapshot', name: error.name, snapPath: error.snapPath};\n\t\t\t\tif (error instanceof VersionMismatchError) {\n\t\t\t\t\timproperUsage.snapVersion = error.snapVersion;\n\t\t\t\t\timproperUsage.expectedVersion = error.expectedVersion;\n\t\t\t\t}\n\n\t\t\t\tthrow fail(new AssertionError(message ?? 'Could not compare snapshot', {\n\t\t\t\t\tasssertion: 't.snapshot()',\n\t\t\t\t\timproperUsage,\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (result.pass) {\n\t\t\t\treturn pass();\n\t\t\t}\n\n\t\t\tif (result.actual) {\n\t\t\t\tthrow fail(new AssertionError(message ?? 'Did not match snapshot', {\n\t\t\t\t\tassertion: 't.snapshot()',\n\t\t\t\t\tformattedDetails: [formatDescriptorDiff(result.actual, result.expected, {invert: true})],\n\t\t\t\t}));\n\t\t\t} else {\n\t\t\t\t// This can only occur in CI environments.\n\t\t\t\tthrow fail(new AssertionError(message ?? 'No snapshot available — new snapshots are not created in CI environments', {\n\t\t\t\t\tassertion: 't.snapshot()',","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L646-L682","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Regenerate snapshots: delete the stale test/snapshots directory or .snap files and re-run with AVA's update flag (npx ava --update-snapshots).","Align the AVA version: if snapVersion != expectedVersion, upgrade (or, with care, downgrade) AVA so the snapshot format matches the existing files.","Check that the snapshot path in improperUsage.snapPath exists, is readable by the test process, and has not been corrupted or hand-edited.","Commit and sync .snap files across branches/CI so all environments use snapshots written by the same AVA version."],"exampleFix":"// before: .snap file written by AVA 2.x run with AVA 4.x\n// terminal\nnpx ava            // AssertionError: Could not compare snapshot\n\n// after\nrm -rf test/snapshots\nnpx ava --update-snapshots   // snapshots rewritten in current format, tests pass","handlingStrategy":"validation","validationCode":"import fs from 'node:fs';\n// Before running snapshot tests, ensure snapshot files exist, are readable, and use the current AVA's format.\nconst snapPath = 'test/snapshots/test.js.snap';\nif (!fs.existsSync(snapPath) || !fs.accessSync(snapPath, fs.constants.R_OK)) {\n  throw new Error(`Snapshot file missing/unreadable: ${snapPath} — run 'npx ava --update-snapshots' locally first`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Commit .snap files so every environment uses snapshots written by the same AVA version.","Upgrade AVA across the whole team/CI at once and regenerate snapshots when the snapshot format version changes.","Never hand-edit .snap files; always regenerate with npx ava --update-snapshots.","Check error.details.improperUsage.snapVersion vs expectedVersion to spot version mismatches quickly."],"tags":["snapshot","ava","snapshot-version-mismatch","test-infrastructure"],"backgroundTag":"snapshot-version-mismatch","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}