# jest-serializer-html

> Jest snapshot serializer that beautifies HTML.

Latest version **7.1.0** (published 2021-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-serializer-html
pnpm add jest-serializer-html
yarn add jest-serializer-html
bun add jest-serializer-html
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.1.0 |
| Published | 2021-07-13 |
| First published | 2017-04-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 53 |
| Author | Raymond RUTJES |
| Maintainers | haroenv, eunjae-lee, francoischalifour, tkrugg, rayrutjes |
| Keywords | jest, snapshot, serializer, html, snapshotSerializer, string, vue, vue.js, test, beautifier, js-beautify |

## Links

- npm: https://www.npmjs.com/package/jest-serializer-html
- Repository: https://github.com/rayrutjes/jest-serializer-html
- Homepage: https://github.com/rayrutjes/jest-serializer-html#readme
- Issues: https://github.com/rayrutjes/jest-serializer-html/issues
- npm.io page: https://npm.io/package/jest-serializer-html

## Dependencies (1)

- [diffable-html](https://npm.io/package/diffable-html.md) ^4.1.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 7.1.0 (latest) — 2021-07-13
- 7.0.0 — 2019-04-28
- 6.0.0 — 2018-12-31
- 5.0.0 — 2018-01-03
- 4.0.1 — 2017-10-18
- 4.0.0 — 2017-05-18
- 3.0.0 — 2017-04-18
- 2.0.0 — 2017-04-17
- 1.0.2 — 2017-04-16
- 1.0.1 — 2017-04-16
- 1.0.0 — 2017-04-14

## README

# A Jest snapshot serializer that beautifies HTML.

[![NPM version](https://badge.fury.io/js/jest-serializer-html.svg)](https://npmjs.org/package/jest-serializer-html)
[![Build Status](https://travis-ci.org/rayrutjes/jest-serializer-html.svg?branch=master)](https://travis-ci.org/rayrutjes/jest-serializer-html)

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](https://github.com/rayrutjes/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:

```bash
# 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](https://facebook.github.io/jest/docs/configuration.html#snapshotserializers-array-string):

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

## Vanilla JS Example

```js
test('should beautify HTML', () => {
  expect('<ul><li><a href="#">My HTML</a></li></ul>').toMatchSnapshot();
});
```

Will output:

```js
exports[`should beautify HTML 1`] = `
<ul>
  <li>
    <a href="#">
      My HTML
    </a>
  </li>
</ul>
`;
```

## Vue.js component output example

```js
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:

```js
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](https://github.com/rayrutjes/diffable-html#readme).

## Special thanks

This package was inspired by the amazing post here: [Jest for all: Episode 1 — Vue.js](https://hackernoon.com/jest-for-all-episode-1-vue-js-d616bccbe186) by [Cristian Carlesso](https://hackernoon.com/@kentaromiura_the_js_guy).

---
_Source: https://npm.io/package/jest-serializer-html · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
