[2.x] Add macros and interceptors to snapshot testing #954
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Description:
The primary purpose of this PR is to ensure snapshot testing works with CSRF tokens and other random data.
As discussed here the goal is handle common types of random data (CSRF token, Livewire wire:id, etc.) within Pest respectively within the Plugin it belongs to.
Besides that the new API is public allowing users to add their own macros and interceptors.
There are two related plugin PRs:
toMatchSnapshot
pest-plugin-laravel#48toMatchSnapshot
on Livewire components pest-plugin-livewire#18Intercept
This allows to intercept the
toMatchSnapshot
method for custom classes. This wouldn't be necessary to achieve the main goal but it makes the handling easier in the Plugins.Example from the Laravel plugin
PestServiceProvider
. See this PR.Example for a custom interceptor:
Nice side effect here, we can get rid of this unrelated
TestResponse
within the Pest core:Macros
This allows to add macros to modify the snapshot string before the match is performed.
From the Livewire plugin
PestServiceProvider
. See this PR.An example for a custom macro:
Users can disable a Macro if they don't want to use it:
Test repository
For an easy overview of all the new possibilities I've setup a test repository.
Documentation
For sure we have to update the documentation.
I am able to help there if you are happy with the decisions I made.
Draft
This and all related Plugin PRs are drafts. I have to clean them up and add more tests when you agree to the API suggested here.
Related:
#946