php test_error ai_generated true

PHPUnit 10.5:快照断言失败异常:快照 "tests/__snapshots__/UserProfileTest__test_render__1.snap" 与预期快照不匹配。使用 --update-snapshots 进行更新。

PHPUnit 10.5: SnapshotAssertionFailedException: Snapshot "tests/__snapshots__/UserProfileTest__test_render__1.snap" does not match the expected snapshot. Use --update-snapshots to update.

ID: php/phpunit-snapshot-assertion-failure

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2024-03-12首次发现

版本兼容性

版本状态引入弃用备注
phpunit/phpunit:10.5.0 active
spatie/phpunit-snapshot-assertions:5.2.0 active

根因分析

磁盘上的快照文件与当前测试输出不一致,通常是由于有意的UI更改、依赖项更新导致渲染HTML变化,或快照文件被意外修改。

English

The snapshot file on disk differs from the current test output, usually due to an intentional UI change, a dependency update altering rendered HTML, or an accidental modification of the snapshot file.

generic

官方文档

https://github.com/spatie/phpunit-snapshot-assertions#updating-snapshots

解决方案

  1. 如果更改是有意的,使用 --update-snapshots 标志运行测试套件以覆盖快照文件:'vendor/bin/phpunit --update-snapshots'
  2. 使用 PHPUnit 默认输出或 'git diff tests/__snapshots__/' 等工具检查差异,确认更改正确后,提交更新后的快照文件。
  3. 如果快照损坏,从版本控制中恢复:'git checkout -- tests/__snapshots__/UserProfileTest__test_render__1.snap'

无效尝试

常见但无效的做法:

  1. 70% 失败

    Deleting the entire __snapshots__ directory and rerunning tests causes all snapshots to be recreated, but this discards the intended baseline for other tests, leading to false positives or missed regressions.

  2. 60% 失败

    Manually editing the snapshot file to match expected output often introduces formatting inconsistencies (e.g., trailing whitespace, line endings), which can cause the assertion to still fail or produce a different diff.

  3. 100% 失败

    Running tests with --verbose or --debug without --update-snapshots does not resolve the mismatch; it only provides more output details but does not update the file.