Skip to content

Commit

Permalink
docs: documentation handling dynamic data
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Jan 25, 2024
1 parent 2dd74b9 commit a0b413c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions snapshot-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ And of course, you can "rebuild" the snapshots at any time by using the `--updat
./vendor/bin/pest --update-snapshots
```

## Handling Dynamic Data

Sometimes, the expected value may contain dynamic data that you cannot control, such as CSRF tokens in a form. In those cases, you can use [Expectation Pipes](/docs/custom-expectations#content-pipe-expectations) to replace that data. Here is an example:

```php
expect()->pipe('toMatchSnapshot', function (Closure $next) {
if (is_string($this->value)) {
$this->value = preg_replace(
'/name="_token" value=".*"/',
'name="_token" value="my_test"',
$this->value
);
}

return $next();
});
```

---

In this chapter, we've seen how powerful snapshot testing is. In the following chapter, we will dive into Pest's custom helpers: [Custom Helpers](/docs/custom-helpers)

0 comments on commit a0b413c

Please sign in to comment.