{"id":"0222ec5a3553ecde","repo":"jestjs/jest","slug":"custom-snapshot-resolver-functions-must-transform","errorCode":null,"errorMessage":"Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${custom.testPathForConsistencyCheck}')) === ${resolvedTestPath}","messagePattern":"Custom snapshot resolver functions must transform paths consistently, i\\.e\\. expects resolveTestPath\\(resolveSnapshotPath\\('(.+?)'\\)\\) === (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-snapshot/src/SnapshotResolver.ts","lineNumber":126,"sourceCode":"\n  verifyConsistentTransformations(customResolver);\n\n  return customResolver;\n}\n\nfunction mustImplement(propName: string, requiredType: string) {\n  return `${chalk.bold(\n    `Custom snapshot resolver must implement a \\`${propName}\\` as a ${requiredType}.`,\n  )}\\nDocumentation: https://jestjs.io/docs/configuration#snapshotresolver-string`;\n}\n\nfunction verifyConsistentTransformations(custom: SnapshotResolver) {\n  const resolvedSnapshotPath = custom.resolveSnapshotPath(\n    custom.testPathForConsistencyCheck,\n  );\n  const resolvedTestPath = custom.resolveTestPath(resolvedSnapshotPath);\n  if (resolvedTestPath !== custom.testPathForConsistencyCheck) {\n    throw new Error(\n      chalk.bold(\n        `Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${custom.testPathForConsistencyCheck}')) === ${resolvedTestPath}`,\n      ),\n    );\n  }\n}\n","sourceCodeStart":108,"sourceCodeEnd":133,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-snapshot/src/SnapshotResolver.ts#L108-L133","documentation":"Thrown by `verifyConsistentTransformations` (SnapshotResolver.ts:120-131) when the custom resolver is not its own inverse: `resolveTestPath(resolveSnapshotPath(testPathForConsistencyCheck))` must equal `testPathForConsistencyCheck`. Jest needs round-trip consistency so it can map a snapshot file back to its test and vice versa without ambiguity.","triggerScenarios":"A resolver that maps `a.test.js` → `__snapshots__/a.snap` but `resolveTestPath('__snapshots__/a.snap')` returns `a.spec.js`. Using different replacement tokens in the two directions. Regex-based resolvers that aren't strict inverses for the consistency check path.","commonSituations":"Hand-written resolver that handles the forward direction but ignores directory layout on the reverse. Resolver copied from another project with a different directory convention.","solutions":["Make `resolveTestPath` the exact inverse of `resolveSnapshotPath` for every input — verify by hand with the consistency check path.","Use the same string replacement in both directions (e.g. swap `.test.` and `.snap.` symmetrically).","Add a unit test that asserts the round trip for several realistic paths.","If you genuinely need a non-invertible mapping, reconsider whether a custom resolver is the right tool."],"exampleFix":"// before — asymmetric mappings\nresolveSnapshotPath: t => t.replace('test', 'snap'),\nresolveTestPath: s => s.replace('snapshot', 'test'),\n\n// after — symmetric, invertible\nresolveSnapshotPath: t => t.replace(/\\.test\\./, '.snap.'),\nresolveTestPath: s => s.replace(/\\.snap\\./, '.test.'),","handlingStrategy":"validation","validationCode":"const r = require('./snapshotResolver');\nconst t = r.testPathForConsistencyCheck;\nconst roundTrip = r.resolveTestPath(r.resolveSnapshotPath(t));\nif (roundTrip !== t) {\n  throw new Error(`Resolver not invertible: ${t} -> ${roundTrip}`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write the two functions as exact inverses and add a property test that round-trips many paths.","Use simple symmetric replacements (e.g. swap extensions) rather than regex transforms that can drop characters.","Re-run the consistency check in a unit test any time you touch the resolver."],"tags":["snapshot-resolver","config","consistency"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}