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

Add snapshot testing to Pest. Assert strings, JSON, and more against stored snapshots using helper functions or Pest expectations like toMatchSnapshot(). Built on Spatie’s phpunit-snapshot-assertions for easy, reliable regression testing.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package as a dev dependency β€” no extra configuration needed:

composer require spatie/pest-plugin-snapshots --dev

After installation, the first snapshot test run will automatically create the tests/Snapshots/ directory and generate snapshot files. Use one of the three core patterns to assert output:

it('outputs correct string', function () {
    $result = (new Order(1))->toString();
    expect($result)->toMatchSnapshot();
});

it('returns expected JSON', function () {
    $response = $this->getJson('/api/orders/1')->json();
    assertMatchesJsonSnapshot($response);
});

Start by reading the README and the underlying phpunit-snapshot-assertions docs β€” this plugin is a thin, API-compatible wrapper.


Implementation Patterns

πŸ” Snapshot Types & Syntax

Use expectation syntax for fluency or function calls for control:

Scenario Pest Expectation Functional
Plain text / HTML expect($html)->toMatchSnapshot() assertMatchesSnapshot($html)
JSON / arrays expect($data)->toMatchJsonSnapshot() assertMatchesJsonSnapshot($data)
Images expect($imagePath)->toMatchImageSnapshot() assertMatchesImageSnapshot($imagePath)

πŸ“ Organizing Snapshots

Group by feature/context using custom IDs to avoid fragile filenames:

it('creates an order', function () {
    $order = Order::create(['amount' => 99.99]);
    expect($order->toArray())->toMatchSnapshot('orders/create');
});

it('updates an order', function () {
    $order->update(['amount' => 149.99]);
    expect($order->toArray())->toMatchSnapshot('orders/update');
});

β†’ Files: tests/Snapshots/Feature/Orders/Order_creates_an_order.orders_create.snapshot

πŸ”„ Development Workflow

  • Update snapshots locally: pest --update-snapshots
  • Verify all snapshots in CI: pest --no-interaction (no prompts, fails on mismatch)
  • Clean unused snapshots: pest --clean-snapshots (v2.3.0+)
  • Diff only failed snapshots: pest --only-failed --diff

Gotchas and Tips

⚠️ Common Pitfalls

  • Test renaming breaks snapshots: Filenames embed the test description. If you rename the test, the old snapshot becomes orphaned. Prefer explicit IDs for stable paths.
  • Whitespace differences cause false failures: Normalize strings explicitly (trim(), preg_replace('/\s+/', ' ', $str)) β€” especially important when generating HTML/Markdown.
  • Image snapshots are fragile across environments: Font rendering, OS, or screen scaling differences may cause mismatches. Use controlled assets or consider tolerance options (check underlying package docs).
  • Never snapshot sensitive data: Snapshots are committed. Sanitize before snapshotting (['secret' => 'REDACTED']) or use custom ID generators to redact.

πŸ›  Debugging & Troubleshooting

  • Check snapshot path: Default is tests/Snapshots/, but can be customized via SNAPSHOT_PATH in phpunit.xml.
  • Verify dependencies: Ensure spatie/phpunit-snapshot-assertions ^5.3.1 matches plugin requirements (v2.3.0+).
  • Snapshot not found? Confirm the test runs with Pest (not PHPUnit) and that tests/Snapshots/ is writable.

πŸ”Œ Customization & Extensibility

  • Custom snapshot ID generator: Use the SnapshotIdAware trait in your test cases or Pest config.
  • Global helper in tests/Pest.php:
    function snapshot_json($data, string $id = ''): void {
        assertMatchesJsonSnapshot($data, $id);
    }
    
  • Snapshot path override in phpunit.xml:
    <php>
        <server name="SNAPSHOT_PATH" value="tests/_snapshots"/>
    </php>
    
  • Per-test directory isolation: Prefix custom IDs with tests/Feature/... to match project structure.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport