What is jest-serializer-html?
The jest-serializer-html package is a Jest serializer for HTML strings. It helps in making HTML snapshots more readable and easier to debug by formatting the HTML content in a more human-readable way.
What are jest-serializer-html's main functionalities?
HTML Serialization
This feature allows you to serialize HTML strings in your Jest tests. By adding the serializer, your HTML snapshots will be formatted in a more readable way, making it easier to identify changes and debug issues.
const serializer = require('jest-serializer-html');
expect.addSnapshotSerializer(serializer);
Customizing Serialization
This feature allows you to customize the serialization process. For example, you can collapse whitespace in the HTML output to make the snapshots more compact.
const serializer = require('jest-serializer-html');
expect.addSnapshotSerializer(serializer({ collapseWhitespace: true }));
Other packages similar to jest-serializer-html
jest-serializer
jest-serializer is a general-purpose serializer for Jest. It allows you to create custom serializers for various data types, including HTML. While it is more flexible, it requires more setup compared to jest-serializer-html, which is specifically designed for HTML.
pretty-format
pretty-format is a package used by Jest internally to format various data types, including HTML. It provides a more general solution for formatting data in Jest snapshots, but it may not be as specialized or straightforward for HTML as jest-serializer-html.
jest-html
jest-html is another package that provides utilities for testing HTML with Jest. It includes matchers and serializers for HTML, offering a more comprehensive solution for HTML testing compared to jest-serializer-html, which focuses solely on serialization.
A Jest snapshot serializer that beautifies HTML.
This serializer is based on js-beautify and is configured to indent HTML tags as much as possible to ease readability of diffs in case of failing snapshot tests.
Install
Add the package as a dev-dependency:
npm install --save-dev jest-serializer-html
yarn add -D jest-serializer-html
Update package.json to let Jest know about the serializer:
"jest": {
"snapshotSerializers": ["jest-serializer-html"]
}
Special thanks
This package was inspired by the amazing post here: Jest for all: Episode 1 — Vue.js by Cristian Carlesso.