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

Phpunit Snapshot Assertions Laravel Package

spatie/phpunit-snapshot-assertions

Add snapshot testing to PHPUnit. Save expected output (JSON, arrays, strings, etc.) on first run and automatically compare on later runs to catch regressions with minimal assertions. Includes handy traits and snapshot update workflow for tests.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Shift from brittle unit tests to maintainable snapshot tests: Reduces flakiness in tests that rely on hardcoded assertions (e.g., API responses, UI outputs) by automating snapshot comparisons.
  • Accelerate frontend/backend integration testing: Ideal for Laravel APIs, CLI tools, or legacy systems where output formats (JSON, XML, HTML) are complex or frequently updated.
  • Build vs. Buy: Buy—eliminates reinventing snapshot testing logic while integrating seamlessly with PHPUnit.
  • Use Cases:
    • API Contract Testing: Validate responses without manually asserting every field (e.g., assertMatchesJsonSnapshot($response->toJson())).
    • CLI/Console Output: Ensure command outputs (e.g., artisan migrate) remain consistent.
    • Legacy System Regression Testing: Protect against unintended changes in monolithic apps.
    • Parallel Testing: Works with Paratest/Laravel’s --parallel via CREATE_SNAPSHOTS=false.
    • Custom Data Formats: Extend with drivers for YAML, XML, or binary files (e.g., images via spatie/pixelmatch-php).

When to Consider This Package

  • Adopt if:
    • Your team writes high-maintenance tests (e.g., API responses with nested objects, dynamic IDs).
    • You need zero-maintenance regression tests for outputs (e.g., emails, PDFs, HTML).
    • Your CI/CD pipeline already uses PHPUnit and Composer.
    • You’re working with Laravel, Symfony, or vanilla PHP projects where output consistency is critical.
  • Look elsewhere if:
    • Your tests are pure unit tests with no output validation (stick with PHPUnit assertions).
    • You need visual regression testing (use tools like Applitools or Percy).
    • Your team lacks CI/CD maturity to handle snapshot updates (requires manual update-snapshots runs).
    • You’re in a JavaScript/TypeScript ecosystem (consider Jest’s snapshot testing).

How to Pitch It (Stakeholders)

For Executives: "This package lets us automate ‘golden master’ testing for APIs, CLI tools, and outputs—reducing test maintenance by 30–50% while catching regressions early. For example, if a developer changes an API response format, the test fails immediately with a diff, saving hours of debugging. It’s a no-code solution that works alongside our existing PHPUnit tests, with zero infrastructure changes. ROI: Faster releases, fewer production bugs."

For Engineers: *"Snapshot testing replaces flaky assertions like assertEquals($expectedJson, $response->toJson()) with automated diffs. Key benefits:

  • First run: Creates a snapshot (test marked ‘incomplete’).
  • Subsequent runs: Fails if output changes (shows diff).
  • Updates: Run composer update-snapshots or UPDATE_SNAPSHOTS=true phpunit to approve changes.
  • Supports: JSON, XML, HTML, files, images (with spatie/pixelmatch-php), and custom drivers. Works with Laravel, Symfony, or vanilla PHP. CI-friendly with CREATE_SNAPSHOTS=false."*

For QA/DevOps: *"This integrates with your existing PHPUnit workflow:

  • Local dev: Snapshots auto-generate on first run.
  • CI: Set CREATE_SNAPSHOTS=false to fail on missing snapshots (no silent creation).
  • Parallel testing: Safe with Paratest/Laravel --parallel via env vars.
  • No false positives: Diffs show exactly what changed (e.g., a field renamed in JSON)."*
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata