You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds three assertion methods to the class:
61
+
62
+
-`assertMatchesSnapshot($actual)`
63
+
-`assertMatchesJsonSnapshot($actual)`
64
+
-`assertMatchesXmlSnapshot($actual)`
65
+
66
+
### Snapshot Testing 101
67
+
68
+
Let's do a snapshot assertion for a simple string, "foo".
69
+
70
+
```php
71
+
public function test_it_is_foo() {
72
+
$this->assertMatchesSnapshot('foo');
73
+
}
74
+
```
75
+
76
+
The first time the assertion runs, it doesn't have a snapshot to compare the string with. The test runner generates a new snapshot and marks the test as incomplete.
77
+
78
+
```
79
+
> ./vendor/bin/phpunit
80
+
81
+
There was 1 incomplete test:
45
82
83
+
1) ExampleTest::test_it_matches_a_string
84
+
Snapshot created for ExampleTest__test_it_matches_a_string
85
+
86
+
OK, but incomplete, skipped, or risky tests!
87
+
Tests: 1, Assertions: 0, Incomplete: 1.
88
+
```
89
+
90
+
Snapshot ids are generated based on the test and testcase's names. Basic snapshots return a `var_export` of the actual value.
91
+
92
+
```php
93
+
<?php return 'foo';
94
+
```
95
+
96
+
Let's rerun the test. The test runner will see that there's already a snapshot for the assertion and do a comparison.
97
+
98
+
```
99
+
> ./vendor/bin/phpunit
100
+
101
+
OK (1 test, 1 assertion)
102
+
```
103
+
104
+
If we change actual value to "bar", the test will fail because the snapshot still returns "foo".
105
+
106
+
```php
107
+
public function test_it_is_foo() {
108
+
$this->assertMatchesSnapshot('bar');
109
+
}
46
110
```
47
-
assertMatchesSnapshot
48
-
assertMatchesJsonSnapshot
49
-
assertMatchesXmlSnapshot
111
+
```
112
+
> ./vendor/bin/phpunit
113
+
114
+
1) ExampleTest::test_it_matches_a_string
115
+
Failed asserting that two strings are equal.
116
+
--- Expected
117
+
+++ Actual
118
+
@@ @@
119
+
-'foo'
120
+
+'bar'
121
+
122
+
FAILURES!
123
+
Tests: 1, Assertions: 1, Failures: 1.
124
+
```
125
+
126
+
When we expect a changed value, we need to tell the test runner to update the existing snapshots instead of failing the test. This is possible by adding a`-d --update-snapshots` flag to the `phpunit` command.
127
+
128
+
```
129
+
> ./vendor/bin/phpunit -d --update-snapshots
130
+
131
+
OK (1 test, 1 assertion)
132
+
```
133
+
134
+
As a result, our snapshot file returns "bar" instead of "foo".
135
+
136
+
```php
137
+
<?php return 'bar';
138
+
```
139
+
140
+
### Customizing Snapshot Ids and Directories
141
+
142
+
Snapshot ids are generated via the `getSnapshotId` method on the `MatchesSnapshot` trait. Override the method to customize the id. By default, a snapshot id exists of the test name, the test case name and an incrementing value, e.g. `Test__my_test_case__1`.
143
+
144
+
#### Example: Replacing the `__` Delimiter With `--`
145
+
146
+
```php
147
+
protected function getSnapshotId(): string
148
+
{
149
+
return (new ReflectionClass($this))->getShortName().'--'.
150
+
$this->getName().'--'.
151
+
$this->snapshotIncrementor;
152
+
}
153
+
```
154
+
155
+
By default, snapshots are stored in a `__snapshots__` directory relative to the test class. This can be changed by overriding the `getSnapshotDirectory` method.
156
+
157
+
#### Example: Renaming the `__snapshots__` directory to `snapshots`
Drivers ensure that different types of data can be serialized and matched in their own way. A driver is a class that implements the `Spatie\Snapshots\Driver` interface, which requires three method implementations: `serialize`, `extension` and `match`.
171
+
172
+
Let's take a quick quick look at the `JsonDriver`.
173
+
174
+
```php
175
+
namespace Spatie\Snapshots\Drivers;
176
+
177
+
use PHPUnit\Framework\Assert;
178
+
use Spatie\Snapshots\Driver;
179
+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
180
+
181
+
class JsonDriver implements Driver
182
+
{
183
+
public function serialize($data): string
184
+
{
185
+
if (! is_string($data)) {
186
+
throw new CantBeSerialized('Only strings can be serialized to json');
- The `serialize` method returns a string which will be written to the snapshot file. In the `JsonDriver`, we'll decode and re-encode the json string to ensure the snapshot has pretty printing.
205
+
- We want to save json snapshots as json files, so we'll use `json` as their file extension.
206
+
- When matching the expected data with the actual data, we want to use PHPUnit's built in json assertions, so we'll call the specific `assertJsonStringEqualsJsonString` method.
207
+
54
208
## Changelog
55
209
56
210
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
0 commit comments