Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
jest-serializer-html
Advanced tools
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.
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 }));
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 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 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.
When using this Jest serializer, it will turn any string starting with '<' to nicely indented HTML in the snapshot.
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.
Add the package as a dev-dependency:
# With npm
npm install --save-dev jest-serializer-html
# With yarn
yarn add -D jest-serializer-html
Update package.json to let Jest know about the serializer:
"jest": {
"snapshotSerializers": ["jest-serializer-html"]
}
test('should beautify HTML', () => {
expect('<ul><li><a href="#">My HTML</a></li></ul>').toMatchSnapshot();
});
Will output:
exports[`should beautify HTML 1`] = `
<ul>
<li>
<a href="#">My HTML</a>
</li>
</ul>
`;
import Vue from 'vue';
const Hello = {
props: {
msg: {
type: String,
default: 'World'
}
},
template: `
<h1>Hello ${ msg }!</h1>
<ul><li><a href="#">My HTML</a></li></ul>
`
};
test('should beautify HTML', () => {
const Component = Vue.extend(Hello);
const vm = new Component({
propsData: {
msg: 'You'
}
});
vm.$mount();
expect(vm.$el.outerHTML).toMatchSnapshot();
});
Will output:
exports[`should beautify HTML 1`] = `
<h1>Hello You!</h1>
<ul>
<li>
<a href="#">My HTML</a>
</li>
</ul>
`;
This package was inspired by the amazing post here: Jest for all: Episode 1 — Vue.js by Cristian Carlesso.
1.0.2 (2017-04-16)
<a name="1.0.1"></a>
FAQs
Jest snapshot serializer that beautifies HTML.
The npm package jest-serializer-html receives a total of 809,904 weekly downloads. As such, jest-serializer-html popularity was classified as popular.
We found that jest-serializer-html demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.