Skip to content

Snapshot Mismatch

错误信息

                Snapshot 做不 match — 运行使用 --updateSnapshot 到更新
              

快速摘要

输出的 你的 code 更改由于 snapshot 是最后保存. 这个能 是 intentional 更改 or bug — 你需要到 review diff.

为什么发生

  • 你 intentionally 更改 code's 输出 (e.g., 更新组件's markup).

  • bug caused unexpected 输出更改.

  • dependency 更新更改输出格式.

  • snapshot 是 corrupted or manually 编辑不正确地.

最小示例

✗ 错误代码
// Component changed but snapshot not updated
test('renders', () => {
  expect(render(<Button label="New" />)).toMatchSnapshot();
  // Fails: snapshot has label="Old"
});
✓ 修复代码
// Review the diff, then update if intentional
// npx jest --updateSnapshot
// Or fix the component if the change is a bug

Review diff 首先. 如果更改是 正确, 更新 snapshot. 如果不, fix code.

如何诊断

  • 读取 diff in 错误输出 — 它 shows 什么更改之间 snapshot and current 输出.

  • Determine 如果更改是 intentional (更新 snapshot) or bug (fix code).

  • 检查 recent code 更改那个可能有 affected 输出.

如何修复

  • 如果 intentional: 运行 jest --updateSnapshot (or jest -u) 到保存新 snapshot.

  • 如果它's bug: fix code 所以输出 matches 保存 snapshot.

  • Review 更新 snapshot file 之前 committing 到 ensure 更改是 正确.

如何预防

  • 总是 review snapshot diffs 之前更新 — 从不 blindly 运行 --updateSnapshot.

  • Use --CI flag in CI 到 prevent accidental snapshot 更新.

  • 提交 snapshot files 到 version control and review 它们 in code reviews.

相关资源

相关术语

相关课程

← 返回语言