Skip to content

A plugin built with pretty-format, useful for removing unnecessary attributes on HTML elements in snapshot tests.

License

Notifications You must be signed in to change notification settings

FedMG/snapshot-serializer-plugin

Repository files navigation

Serializer Plugin for Snapshot Tests

This is a useful serializer plugin for Jest snapshot testing to give more readable snapshot outputs and improve your tests based on the Testing Best Practices, and which uses the Pretty Format library.

Usage

For the moment, you can use it by doing a fork and installing the dependencies or by copying the snapshotSerializer.
You will need to configure it in your Jest configuration file.
Here's an example:

Jest config file property with the name "snapshotSerializer" that require an array with one or more strings that references to plugin

Once you have configured the plugin, any snapshot tests that are run with Jest will use the pretty-format serializer.

Example

Here's an example test that uses the serializer to generate a more readable snapshot output:

Alt text

As you can see, the output is much easier to read and understand than the default snapshot output.
Also, with this plugin we filter useless attributes and we avoid to update the snapshot any time we have modified the attributes.

Customization of filtered attributes

You can to exclude certain attributes from the serialized output. You can do this by adding the attributes to the VALID_ATTRIBUTES constant of the serializer.

A constant array named 'VALID_ATTRIBUTES' where only has one string by default the 'data-testid' attribute

Conclusion

With the snapshot serializer plugin using pretty-format, Jest snapshot tests become much more readable and easier to understand. In addition, we filter out the attributes that are not needed to avoid having to update the snapshot every time we modify the attributes of an element. You can tailor the output to your specific needs.

About

A plugin built with pretty-format, useful for removing unnecessary attributes on HTML elements in snapshot tests.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published