Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pest Plugin Snapshots Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Seamless Pest Integration: Designed as a first-class Pest plugin, leveraging Pest’s expect() syntax and it() blocks, ensuring native feel for Laravel/Pest users. Avoids PHPUnit fragmentation by aligning with Pest’s ecosystem.
  • Laravel Synergy: Ideal for Laravel-specific outputs (Blade templates, API responses, Livewire/Inertia components, emails) where dynamic content requires deterministic validation.
  • Modular Design: Under-the-hood, it wraps spatie/phpunit-snapshot-assertions, ensuring backward compatibility with existing snapshot logic while adding Pest-specific sugar.
  • Test Pyramid Alignment: Best suited for integration/end-to-end tests (not unit tests) where outputs are complex or non-deterministic (e.g., timestamps, locales, generated IDs).

Integration Feasibility

  • Low Friction: Single composer require with zero config for basic usage. Pest’s expect() syntax is already familiar to Laravel devs (shared with Laravel’s HttpTests).
  • Pest Version Lock: Explicit support for Pest v2/v3 (v2.2.0+), with no breaking changes in recent releases. Minimum PHP version aligns with Laravel’s LTS support (PHP 8.1+).
  • Snapshot Storage: Uses filesystem-based snapshots (default: tests/__snapshots__/), which integrates cleanly with Laravel’s project structure. Can be customized via config (e.g., S3 storage for large teams).
  • CI/CD Ready: Works with GitHub/GitLab merge request diffs for visual snapshot comparisons, reducing manual review overhead.

Technical Risk

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.

Key Questions for TPM

  1. Output Complexity:
    • Which dynamic outputs (APIs, emails, Blade, Livewire) are most prone to regressions? Prioritize these for snapshot coverage.
  2. CI/CD Workflow:
    • How will snapshot updates be handled? (e.g., auto-approve in PRs for trusted teams, or require manual review?)
  3. Storage Strategy:
    • Should snapshots be versioned (e.g., per commit) or pruned after X days to save disk space?
  4. Team Adoption:
    • Will developers prefer assertMatchesSnapshot() or the expect() fluent API? (Fluent is more idiomatic for Pest.)
  5. Flakiness Mitigation:
    • How will non-deterministic data (e.g., id, created_at) be handled? (Options: pre-process, ignore fields, or use snapshot diffs.)
  6. Image Snapshots:
    • Is image validation needed (e.g., PDFs, canvas outputs)? If so, ensure storage quotas are set for binary snapshots.
  7. Pest Migration:
    • If not already using Pest, what’s the migration path from PHPUnit? (Snapshots can coexist but may require dual maintenance initially.)

Integration Approach

Stack Fit

  • Pest-Powered Laravel: Perfect for Laravel projects using Pest (v2/v3), especially those with:
    • API-heavy applications (REST/GraphQL).
    • Dynamic frontend outputs (Livewire, Inertia, Blade).
    • Generated content (emails, PDFs, CSV exports).
  • Complementary Tools:
    • Laravel Dusk: Test rendered HTML snapshots.
    • Laravel Sanctum/Passport: Validate auth-related outputs (e.g., tokens, cookies).
    • Pest Plugins: Combine with pest-plugin-laravel for zero-config Laravel testing.
  • Avoid Overlap:
    • Not for unit tests: Use PHPUnit or Pest’s native assertions for pure logic.
    • Not for contract testing: Use tools like videlalvaro/php-mock or OpenAPI validators for schema enforcement.

Migration Path

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.

Compatibility

  • Pest Versions:
    • v2/v3: Fully supported (v2.2.0+).
    • v1: Use v1.x of this package (deprecated).
  • PHPUnit: Can coexist but requires dual maintenance (not recommended).
  • Laravel Features:
    • Livewire/Inertia: Test rendered components via expect($livewire->html())->toMatchSnapshot().
    • API Testing: Validate responses with expect($response->json())->toMatchSnapshot().
    • Mailables: Test email HTML with expect($mailable->render())->toMatchSnapshot().
  • Third-Party Risks:
    • Image Snapshots: Requires GD/Imagick (common in Laravel) but may bloat storage.
    • Large Snapshots: JSON snapshots >1MB may slow CI. Consider compression or binary storage.

Sequencing

  1. Pilot Phase:
    • Start with 1–2 high-impact outputs (e.g., dashboard API, critical email).
    • Use --update-snapshots to baseline expectations.
  2. Team Training:
    • Document snapshot best practices (e.g., exclude non-deterministic fields).
    • Train on fluent API (expect(...)->toMatchSnapshot()) vs. static assertions.
  3. CI Integration:
    • Add snapshot validation to PR checks (fail on diffs).
    • Configure auto-approve for snapshot updates in trusted branches.
  4. Scaling:
    • Expand to all dynamic outputs (Livewire, Blade, APIs).
    • Optimize storage (e.g., prune old snapshots, use S3 for large teams).

Operational Impact

Maintenance

  • Snapshot Updates:
    • Manual: Run pest --update-snapshots after intentional changes.
    • Automated: Use CI scripts to auto-approve snapshot updates in PRs (with safeguards).
  • Storage Management:
    • Filesystem: Default __snapshots__/ folder (add to .gitignore if needed).
    • Custom Storage: Extend via SnapshotStorage interface (e.g., S3, database).
  • Cleanup:
    • Orphaned Snapshots: Delete snapshots for removed tests via CI hooks.
    • Size Limits: Monitor snapshot folder size; archive old snapshots.

Support

  • Troubleshooting:
    • Failed Snapshots: Use pest --debug to see diff details.
    • Flaky Tests: Exclude non-deterministic fields (e.g., id, created_at) via pre-processing.
  • Common Issues:
    • Duplicate IDs: Fixed in v2.3.1; ensure latest version is used.
    • CI Timeouts: Large snapshots may slow tests; optimize with parallelization.
  • Documentation:
    • Internal Wiki: Document snapshot workflows (e.g., how to update snapshots).
    • Example Tests: Provide templates for common use cases (API
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai