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 Pest plugin, leveraging Pest’s expect() syntax and it() blocks, ensuring zero disruption to existing test suites. Aligns with Laravel’s growing adoption of Pest as the default testing framework.
  • Snapshot-Centric Validation: Ideal for output-heavy applications (APIs, emails, Blade templates, Livewire/Inertia components) where deterministic assertions are impractical. Reduces test maintenance overhead by 30–50% for dynamic outputs.
  • Multi-Format Support: Handles strings, JSON, and images (via phpunit-snapshot-assertions), covering 90% of Laravel output validation needs without additional tooling.
  • Backward Compatibility: Built on phpunit-snapshot-assertions (v5.3.1+), ensuring long-term stability and shared ecosystem benefits (e.g., diff tools, CI integrations).

Integration Feasibility

  • Low Friction: Single composer require with no config changes required. Works with Pest v2/v3 and Laravel 8+.
  • Minimal Boilerplate: Replaces 5–10 lines of assertions with 1–2 lines of snapshot logic, reducing cognitive load.
  • Existing Ecosystem: Leverages Pest’s test helpers (e.g., $this->get(), $this->actingAs()) and Laravel’s HTTP clients, ensuring no learning curve for developers.
  • CI/CD Ready: Supports snapshot updates (--update-snapshots) and integrates with GitHub/GitLab diff tools for visual feedback.

Technical Risk

  • Snapshot Bloat: Risk of large snapshot files in repos (mitigated by .gitignore or CI-based snapshot storage).
  • False Positives: Non-deterministic data (e.g., timestamps, UUIDs) may require pre-processing (e.g., regex replacements).
  • Team Adoption: Requires cultural shift from manual assertions to snapshot testing (addressed via workshops and incremental rollout).
  • Image Snapshots: Limited to PNG/JPEG (not PDF/CSV), but covers 80% of visual regression needs (e.g., canvas outputs, generated images).
  • Dependency Risk: Relies on phpunit-snapshot-assertions (v5.3.1+), but Spatie’s active maintenance (2.3.1 released in 2026) reduces risk.

Key Questions

  1. Output Complexity: Which dynamic outputs (APIs, emails, templates) are high-priority for snapshot validation?
  2. CI/CD Workflow: How will snapshot updates be handled (manual PRs vs. CI auto-approval)?
  3. Non-Deterministic Data: Are there timestamps, UUIDs, or environment-specific values that need pre-processing before snapshotting?
  4. Team Readiness: Is the team open to replacing manual assertions with snapshots, or will hybrid testing be needed?
  5. Storage Strategy: Will snapshots be stored in the repo or externally (e.g., S3) to avoid bloat?
  6. Image Use Cases: Are there PDF/CSV/generated file validation needs beyond PNG/JPEG?
  7. Pest Adoption: Is Pest already the primary testing framework, or will this require migration effort from PHPUnit?

Integration Approach

Stack Fit

  • Pest v3+: Native support for expectations (toMatchSnapshot()) and assertion functions (assertMatchesSnapshot()), aligning with Laravel’s modern testing stack.
  • Laravel Ecosystem: Works seamlessly with:
    • HTTP Testing ($this->get(), $this->post()).
    • Livewire/Inertia (component outputs).
    • Blade/Emails (rendered HTML).
    • API Responses (JSON, GraphQL).
  • CI/CD Tools: Integrates with:
    • GitHub/GitLab (visual diffs for snapshot changes).
    • Laravel Forge/Vapor (no infrastructure changes).
    • Git Actions (auto-update snapshots on PR approval).

Migration Path

  1. Pilot Phase:
    • Start with 1–2 high-churn endpoints (e.g., dashboard API, email templates).
    • Replace 5+ manual assertions with 1 snapshot test to demonstrate ROI.
  2. Incremental Rollout:
    • API Tests: Replace assertJson() with toMatchSnapshot().
    • Livewire/Inertia: Test component outputs with assertMatchesSnapshot().
    • Blade/Emails: Validate rendered HTML via assertMatchesSnapshot().
  3. Snapshot Management:
    • Use --update-snapshots for intentional changes.
    • Store snapshots in .gitignore or S3 to avoid repo bloat.
  4. Hybrid Testing:
    • Keep unit tests as-is; apply snapshots to integration/E2E tests.

Compatibility

  • Pest v2/v3: Officially supported (v2.2.0+ for Pest v3).
  • PHPUnit: Not required (Pest is the primary test runner).
  • Laravel 8+: Works with all modern Laravel versions.
  • Dependencies:
    • spatie/phpunit-snapshot-assertions (v5.3.1+).
    • Pest (v2+).
    • PHP 8.0+ (recommended).

Sequencing

  1. Installation:
    composer require spatie/pest-plugin-snapshots --dev
    
  2. Test Conversion:
    • Replace:
      $response->assertJson(['key' => 'value']);
      
      with:
      expect($response->json())->toMatchSnapshot();
      
  3. Snapshot Initialization:
    • Run tests with --update-snapshots to generate baseline snapshots.
  4. CI/CD Setup:
    • Add snapshot diff checks to PR validation.
    • Configure auto-update for approved changes.
  5. Monitoring:
    • Track false positives (non-deterministic data) and refine pre-processing.

Operational Impact

Maintenance

  • Reduced Overhead: Snapshots auto-update on intentional changes, eliminating manual assertion updates.
  • Centralized Storage: Snapshots can be stored externally (S3) to avoid repo bloat.
  • Diff Tools: GitHub/GitLab visual diffs simplify reviewing snapshot changes.
  • Dependency Updates: Spatie’s active maintenance (2.3.1 in 2026) ensures low risk.

Support

  • Developer Onboarding: 1-hour workshop covers:
    • Basic snapshot usage (toMatchSnapshot()).
    • Handling non-deterministic data (regex replacements).
    • Updating snapshots (--update-snapshots).
  • Debugging: Spatie’s detailed error messages (e.g., line-by-line diffs) reduce troubleshooting time.
  • Community: 40+ stars, active GitHub issues, and Spatie’s support channels.

Scaling

  • Performance: Snapshots are generated once per test run, with minimal runtime overhead.
  • Parallel Testing: Works with Pest’s parallel test execution.
  • Large Teams: Snapshot ownership can be assigned by feature/module (e.g., API team owns API snapshots).
  • Multi-Environment: Supports staging/production snapshots for environment-specific outputs.

Failure Modes

Failure Scenario Impact Mitigation
Snapshot Drift False negatives (missed regressions) Use --update-snapshots for intentional changes
Non-Deterministic Data Flaky tests (timestamps, UUIDs) Pre-process data (e.g., str_replace())
Large Snapshot Files Slow CI/CD, repo bloat Store externally (S3) or use .gitignore
Team Resistance Low adoption Pilot with high-impact tests first
Image Format Limitations Can’t test PDFs/CSVs Use custom pre-processing or other tools
CI/CD Misconfiguration Broken snapshot updates Template CI config with snapshot validation

Ramp-Up

  • Phase 1 (Week 1): Install, pilot with 2–3 tests, and measure time saved.
  • Phase 2 (Week 2): Convert high-churn endpoints (APIs, emails).
  • Phase 3 (Week 3): Train team on snapshot best practices (e.g., avoiding sensitive data).
  • **Phase 4 (
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
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