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
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| phpunit/phpunit:10.5.0 | active | — | — | — |
| spatie/phpunit-snapshot-assertions:5.2.0 | active | — | — | — |
Root Cause
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中文
磁盘上的快照文件与当前测试输出不一致,通常是由于有意的UI更改、依赖项更新导致渲染HTML变化,或快照文件被意外修改。
Official Documentation
https://github.com/spatie/phpunit-snapshot-assertions#updating-snapshotsWorkarounds
-
95% success If the change is intentional, run the test suite with the --update-snapshots flag to overwrite the snapshot file: 'vendor/bin/phpunit --update-snapshots'
If the change is intentional, run the test suite with the --update-snapshots flag to overwrite the snapshot file: 'vendor/bin/phpunit --update-snapshots'
-
90% success Inspect the diff using PHPUnit's default output or a tool like 'git diff tests/__snapshots__/' to verify the change is correct, then commit the updated snapshot file.
Inspect the diff using PHPUnit's default output or a tool like 'git diff tests/__snapshots__/' to verify the change is correct, then commit the updated snapshot file.
-
85% success If the snapshot is corrupted, restore it from version control: 'git checkout -- tests/__snapshots__/UserProfileTest__test_render__1.snap'
If the snapshot is corrupted, restore it from version control: 'git checkout -- tests/__snapshots__/UserProfileTest__test_render__1.snap'
中文步骤
如果更改是有意的,使用 --update-snapshots 标志运行测试套件以覆盖快照文件:'vendor/bin/phpunit --update-snapshots'
使用 PHPUnit 默认输出或 'git diff tests/__snapshots__/' 等工具检查差异,确认更改正确后,提交更新后的快照文件。
如果快照损坏,从版本控制中恢复:'git checkout -- tests/__snapshots__/UserProfileTest__test_render__1.snap'
Dead Ends
Common approaches that don't work:
-
70% fail
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.
-
60% fail
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.
-
100% fail
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.