Snapshots Plugin
Overview
The Snapshots Plugin for Pest adds snapshot testing capabilities to Pest.
Source code: github.com/spatie/pest-plugin-snapshots
Snapshot tests are a very useful tool whenever you want to make sure things don't change unexpectedly. Find more about snapshot testing here: sebastiandedeyne.com/a-package-for-snapshot-testing-in-phpunit/
Installation
Install the Snapshots Plugin via the Composer package manager:
1composer require spatie/pest-plugin-snapshots --dev
Available functions
assertMatchesSnapshot()
The assertMatchesSnapshot()
function asserts the given string matches the existing snapshot.
1use function Spatie\Snapshots\assertMatchesSnapshot;2use function Pest\Laravel\get;3 4it('renders correctly', function () {5 $html = get('/')->getContent();6 7 assertMatchesSnapshot($html);8});
If you’re working with specific data like JSON or XML, you’re better off using a dedicated assertMatchesJsonSnapshot
or assertMatchesXmlSnapshot
method, which will save snapshots as .json
or .xml
files, and provide a better diff when the snapshot doesn’t match:
-
assertMatchesSnapshot()
-
assertMatchesFileHashSnapshot()
-
assertMatchesFileSnapshot()
-
assertMatchesHtmlSnapshot()
-
assertMatchesJsonSnapshot()
-
assertMatchesObjectSnapshot()
-
assertMatchesTextSnapshot()
-
assertMatchesXmlSnapshot()
-
assertMatchesYamlSnapshot()
Note that, by default, snapshots are stored in a tests/__snapshots__
directory.
Also, when you expect a changed value, you may need to run the -d --update-snapshots
flag to update the existing snapshots:
1./vendor/bin/pest -d --update-snapshots
Next section: Time Plugin →