spatie/pest-plugin-snapshots
Adds snapshot testing to Pest via Spatie’s snapshot assertions. Compare strings or JSON against stored snapshots with helper functions or Pest expectations. Ideal for stable output/regression testing in PHP projects.
expect() syntax and it() blocks, ensuring native feel for Laravel/Pest users. Avoids PHPUnit fragmentation by aligning with Pest’s ecosystem.spatie/phpunit-snapshot-assertions, ensuring backward compatibility with existing snapshot logic while adding Pest-specific sugar.composer require with zero config for basic usage. Pest’s expect() syntax is already familiar to Laravel devs (shared with Laravel’s HttpTests).tests/__snapshots__/), which integrates cleanly with Laravel’s project structure. Can be customized via config (e.g., S3 storage for large teams).| Risk Area | Mitigation Strategy |
|---|---|
| Snapshot Bloat | Use .gitignore for __snapshots__/ or prune old snapshots via CI scripts. |
| Flaky Tests | Exclude non-deterministic data (e.g., created_at) via pre-processing or snapshot diffs. |
| Merge Conflicts | Enforce snapshot updates in feature branches (not main) to avoid CI pollution. |
| Performance | Snapshots are generated once per test run; no runtime overhead for passing tests. |
| Tooling Lock-in | Snapshots are human-readable (JSON/strings) and can be migrated if needed. |
| Image Snapshots | Requires GD/Imagick (common in Laravel) but adds storage bloat for binary data. |
assertMatchesSnapshot() or the expect() fluent API? (Fluent is more idiomatic for Pest.)id, created_at) be handled? (Options: pre-process, ignore fields, or use snapshot diffs.)pest-plugin-laravel for zero-config Laravel testing.videlalvaro/php-mock or OpenAPI validators for schema enforcement.| Current State | Migration Steps |
|---|---|
| No Pest | 1. Migrate to Pest (composer require pestphp/pest --dev). 2. Replace PHPUnit assertions with Pest’s expect(). 3. Add snapshots incrementally. |
| PHPUnit Snapshots | 1. Install spatie/pest-plugin-snapshots. 2. Rewrite tests to use Pest’s expect() syntax. 3. Delete PHPUnit snapshot configs. |
| Custom Snapshot Logic | 1. Replace custom scripts with assertMatchesSnapshot(). 2. Migrate snapshot storage to __snapshots__/. 3. Deprecate old logic. |
| Manual Assertions | 1. Identify high-churn outputs (e.g., API responses, emails). 2. Replace assertEquals() with snapshots. 3. Use --update-snapshots to baseline. |
expect($livewire->html())->toMatchSnapshot().expect($response->json())->toMatchSnapshot().expect($mailable->render())->toMatchSnapshot().--update-snapshots to baseline expectations.expect(...)->toMatchSnapshot()) vs. static assertions.pest --update-snapshots after intentional changes.__snapshots__/ folder (add to .gitignore if needed).SnapshotStorage interface (e.g., S3, database).pest --debug to see diff details.id, created_at) via pre-processing.How can I help you explore Laravel packages today?