- How do I add snapshot testing to my Laravel project using Pest?
- Install the package via Composer with `composer require spatie/pest-plugin-snapshots --dev`. No additional configuration is needed—it integrates seamlessly with Pest’s existing syntax. Use `assertMatchesSnapshot()` or Pest’s `expect()->toMatchSnapshot()` in your tests.
- Does this work with Laravel 8+ and Pest v2/v3?
- Yes, the package officially supports Pest v2 and v3, as well as Laravel 8+. It’s designed to work without conflicts, leveraging Pest’s modern testing features while maintaining backward compatibility with PHPUnit-based assertions.
- Can I test JSON API responses with snapshots?
- Absolutely. Use `assertMatchesJsonSnapshot()` or `expect($response->json())->toMatchSnapshot()` to validate JSON structures. It’s ideal for APIs, GraphQL responses, or any JSON-heavy output where manual assertions are cumbersome.
- How do I handle dynamic data like timestamps or UUIDs in snapshots?
- Pre-process your data before snapshotting. For example, use regex replacements or custom functions to strip out timestamps, UUIDs, or environment-specific values. The underlying `phpunit-snapshot-assertions` package supports this via `assertMatchesSnapshot()` with custom diff logic.
- Will snapshots bloat my Git repository?
- Snapshot files can grow large, but you can mitigate this by excluding them from Git (add to `.gitignore`) or storing them externally (e.g., S3). The package also supports `--update-snapshots` for CI/CD workflows to manage updates without cluttering your repo.
- Can I use this for testing Blade templates or Livewire/Inertia components?
- Yes, it’s perfect for Blade templates, emails, and Livewire/Inertia outputs. Capture the rendered HTML or component output as a string and compare it against snapshots. Works alongside Laravel’s HTTP testing helpers like `$this->get()` or `$this->actingAs()`.
- How do I update snapshots in CI without manual PRs?
- Use the `--update-snapshots` flag in your CI pipeline to auto-update snapshots when tests pass. Tools like GitHub/GitLab can show visual diffs of snapshot changes, making it easier to review and approve updates. Combine this with branch protection rules for safety.
- Are there alternatives to this package for Laravel snapshot testing?
- The primary alternative is `phpunit-snapshot-assertions` directly, but this package wraps it for Pest users, offering a more integrated experience. Other options include custom solutions with `assertStringEqualsFile()` or third-party tools like `laravel-snapshots`, but none match this package’s Pest-native simplicity.
- Does this support image snapshots (e.g., for canvas outputs or generated images)?
- Yes, it supports PNG and JPEG image snapshots via the underlying `phpunit-snapshot-assertions` package. This is useful for visual regression testing of canvas outputs, charts, or generated images in Laravel apps. Note that PDF/CSV support is limited to text-based comparisons.
- How do I migrate from PHPUnit to Pest with snapshot testing?
- Start by replacing manual assertions (e.g., `assertJson()`) with `toMatchSnapshot()` in Pest. Use the package’s helper functions like `assertMatchesSnapshot()` for a 1:1 swap. For large projects, pilot the migration on high-churn endpoints first to demonstrate ROI before full adoption.