Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jest-serializer-html

rayrutjes3.9mMIT7.1.0

Jest snapshot serializer that beautifies HTML.

jest, snapshot, serializer, html, snapshotSerializer, string, vue, vue.js, test, beautifier, js-beautify

readme

A Jest snapshot serializer that beautifies HTML.

NPM version Build Status

When using this Jest serializer, it will turn any string starting with '<' to nicely indented HTML in the snapshot.

This serializer is based on diffable-html which is an opinionated HTML formatter that will ease readability of diffs in case of failing snapshot tests.

Install

Add the package as a dev-dependency:

# With npm
npm install --save-dev jest-serializer-html

# With yarn
yarn add --dev jest-serializer-html

Update package.json to let Jest know about the serializer:

"jest": {
  "snapshotSerializers": ["jest-serializer-html"]
}

Vanilla JS Example

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>
`;

Vue.js component output example

import Vue from 'vue';
const Hello = {
  props: {
    msg: {
      type: String,
      default: 'World'
    }
  },
  template: `
    <h1>Hello ${ msg }!</h1>
    <ul id="main-list" class="list"><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 id="main-list"
    class="list"
>
  <li>
    <a href="#">
      My HTML
    </a>
  </li>
</ul>
`;

You can read more about the HTML formatting here.

Special thanks

This package was inspired by the amazing post here: Jest for all: Episode 1 — Vue.js by Cristian Carlesso.

changelog

7.1.0 (2021-07-13)

Features

7.0.0 (2019-04-28)

Features

6.0.0 (2018-12-31)

Features

  • better handling of whitespaces (0bf9273)

5.0.0 (2018-01-03)

Features

4.0.1 (2017-10-18)

Bug Fixes

  • better detect HTML by trimming string before checking for < (aabe2e1)

4.0.0 (2017-05-18)

Features

  • formatting: upgrade diffable-html dependency to benefit from directive and comments outputting (ad30c8c)

3.0.0 (2017-04-18)

Features

  • html-format: better default HTML formatting that ameliorates diff readability (3470ddd)

2.0.0 (2017-04-17)

Features

  • html-format: fix indent size to 2 spaces by default (40598cb)
  • html-format: force each tag attribute to be on its own line (28f861b)

1.0.2 (2017-04-16)

1.0.1 (2017-04-16)

1.0.0 (2017-04-14)