$ npm install --save-dev enzyme-to-json
The serializer is the recommended way to use
enzyme-to-json, the installation and usage of it is very easy and allows you to write clean and simple snapshot tests.
In order to use the serializer, just add this line to your Jest configuration:
For most projects, that is all you need to start using snapshot tests on Enzyme wrappers. The rest of this readme is only showing advanced usages of this library.
In case you are still confused, here is a minimal example project demonstrating this configuration.
Serializer in unit tests
You can add the serializer for only one Jest test file by adding these lines at the beginning of your Jest unit test file:
You can also add the serializer for all tests using the
setupTestFrameworkScriptFile configuration option from Jest.
At the beginning,
enzyme-to-json was just a helper because serializers weren't supported by Jest. Even though it is now recommended to use the serializer to keep your tests simple, you can still use the helper as it gives you access to the option objects.
The helper is just a function you can import from
enzyme-to-json and just pass your Enzyme wrapper as the first parameter and snapshot test the returned value, you'll get the same results as if you used the serializer. Note that you don't have to disable the serializer if you had configured it for the rest of your project. Here is a usage example:
The main purpose of using the helper is to use the option object. The option object is just the second argument of the helper, here is an example:
And here are all the possible options:
||You can change each nested node of your component output by providing the map option. See