UNPKG

5.29 kBMarkdownView Raw
1# enzyme-to-json
2
3[![Build Status](https://img.shields.io/travis/adriantoine/enzyme-to-json.svg?branch=master&style=flat-square)](https://travis-ci.org/adriantoine/enzyme-to-json)
4[![codecov](https://img.shields.io/codecov/c/github/adriantoine/enzyme-to-json.svg?style=flat-square)](https://codecov.io/gh/adriantoine/enzyme-to-json)
5[![npm Version](https://img.shields.io/npm/v/enzyme-to-json.svg?style=flat-square)](https://www.npmjs.com/package/enzyme-to-json)
6[![License](https://img.shields.io/npm/l/enzyme-to-json.svg?style=flat-square)](https://www.npmjs.com/package/enzyme-to-json)
7[![Downloads](https://img.shields.io/npm/dm/enzyme-to-json.svg?style=flat-square)](https://npm-stat.com/charts.html?package=enzyme-to-json)
8
9Convert [Enzyme](http://airbnb.io/enzyme/) wrappers to a format compatible with [Jest snapshot testing](https://facebook.github.io/jest/docs/tutorial-react.html#snapshot-testing).
10
11# Install
12
13```console
14$ npm install --save-dev enzyme-to-json
15```
16
17# Usage
18
19The serializer is the recommended way to use `enzyme-to-json`, the installation and usage of it is very easy and allows you to write clean and simple snapshot tests.
20
21In order to use the serializer, just add this line to your [Jest configuration](https://facebook.github.io/jest/docs/en/configuration.html):
22
23```js
24"snapshotSerializers": ["enzyme-to-json/serializer"]
25```
26
27[Example](https://github.com/adriantoine/enzyme-to-json-v3-testing/blob/master/package.json#L25-L29)
28
29For most projects, that is all you need to start using snapshot tests on Enzyme wrappers. The rest of this readme is only showing advanced usages of this library.
30
31In case you are still confused, [here is a minimal example project](https://github.com/adriantoine/enzyme-to-json-v3-testing) demonstrating this configuration.
32
33# Advanced usage
34
35## Serializer in unit tests
36
37You can add the serializer for only one Jest test file by adding these lines at the beginning of your Jest unit test file:
38
39```js
40import {createSerializer} from 'enzyme-to-json';
41
42expect.addSnapshotSerializer(createSerializer({mode: 'deep'}));
43```
44
45You can also add the serializer for all tests using the [`setupFilesAfterEnv`](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array) configuration option from Jest.
46
47## Helper
48
49At the beginning, `enzyme-to-json` was just a helper because serializers weren't supported by Jest. Even though it is now recommended to use the serializer to keep your tests simple, you can still use the helper as it gives you access to the option objects.
50
51The helper is just a function you can import from `enzyme-to-json` and just pass your Enzyme wrapper as the first parameter and snapshot test the returned value, you'll get the same results as if you used the serializer. Note that you don't have to disable the serializer if you had configured it for the rest of your project. Here is a usage example:
52
53```js
54import React, {Component} from 'react';
55import {shallow} from 'enzyme';
56import toJson from 'enzyme-to-json';
57
58it('renders correctly', () => {
59 const wrapper = shallow(
60 <MyComponent className="my-component">
61 <strong>Hello World!</strong>
62 </MyComponent>,
63 );
64
65 expect(toJson(wrapper)).toMatchSnapshot();
66});
67```
68
69The main purpose of using the helper is to use the option object. The option object is just the second argument of the helper, here is an example:
70
71```js
72toJson(wrapper, {
73 noKey: false,
74 mode: 'deep',
75});
76```
77
78And here are all the possible options:
79
80| Key | Value | Description |
81| -------------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
82| `noKey` | `bool` | Since `v2.0.0`, the `key` prop is included in the snapshot, you can turn it off if you don't want your key to be in your snapshot by settting this option to `true`. Only works for the `mount` and `shallow` wrappers. |
83| `mode` | `'deep'`, `'shallow'` | The `deep` option will return a test object rendered to **maximum** depth while the `shallow` option will return a test object rendered to **minimum** depth. Only works for the `mount` wrappers. See `mode` documentation for examples. |
84| `map` | `function` | You can change each nested node of your component output by providing the map option. See `map` documentation for examples. |
85| `ignoreDefaultProps` | `bool` | You can exclude the default props from snapshots in shallow mode |
86
87# Docs
88
89- [Helper functions](/docs/helper-functions.md)
90- [Modes](/docs/modes.md)
91- [Map](/docs/map.md)