All Projects → adriantoine → Enzyme To Json

adriantoine / Enzyme To Json

Licence: mit
Snapshot test your Enzyme wrappers

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Enzyme To Json

Enzyme Matchers
Jasmine/Jest assertions for enzyme
Stars: ✭ 881 (-7.65%)
Mutual labels:  jest, unit-testing, enzyme
react-testing-talk
No description or website provided.
Stars: ✭ 12 (-98.74%)
Mutual labels:  unit-testing, enzyme, jest
testing-reactjs-examples
🧪 "What should we test in our React components" - presentation examples.
Stars: ✭ 23 (-97.59%)
Mutual labels:  unit-testing, enzyme, jest
React Ssr Starter
All have been introduced React environment
Stars: ✭ 20 (-97.9%)
Mutual labels:  jest, enzyme
Relay Testing Utils
Easy to use relay mock and unit testing tool (works with Jest & Enzyme)
Stars: ✭ 13 (-98.64%)
Mutual labels:  jest, enzyme
Snapguidist
Snapshot testing for React Styleguidist
Stars: ✭ 287 (-69.92%)
Mutual labels:  jest, snapshot-testing
Sazerac
Data-driven unit testing for Jasmine, Mocha, and Jest
Stars: ✭ 322 (-66.25%)
Mutual labels:  jest, unit-testing
Webpack React Boilerplate
Minimal React 16 and Webpack 4 boilerplate with babel 7, using the new webpack-dev-server, react-hot-loader, CSS-Modules
Stars: ✭ 358 (-62.47%)
Mutual labels:  jest, enzyme
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+1973.48%)
Mutual labels:  jest, enzyme
Snapshot Diff
Diffing snapshot utility for Jest
Stars: ✭ 490 (-48.64%)
Mutual labels:  jest, snapshot-testing
Youtube React
A Youtube clone built in React, Redux, Redux-saga
Stars: ✭ 421 (-55.87%)
Mutual labels:  jest, enzyme
Jasmine Matchers
Write Beautiful Specs with Custom Matchers for Jest and Jasmine
Stars: ✭ 552 (-42.14%)
Mutual labels:  jest, unit-testing
React Native Navigation Redux Starter Kit
React Native Navigation(v2) Starter Kit with Redux, Saga, ESLint, Babel, Jest and Facebook SDK 😎
Stars: ✭ 271 (-71.59%)
Mutual labels:  jest, unit-testing
Simple React App
Simple base app using react, react-router v4, hot-reload & sass.
Stars: ✭ 263 (-72.43%)
Mutual labels:  jest, enzyme
React Redux Boilerplate
Awesome React Redux Workflow Boilerplate with Webpack 4
Stars: ✭ 307 (-67.82%)
Mutual labels:  jest, enzyme
react-boilerplate
Sets the ground up for CRA-like projects.
Stars: ✭ 23 (-97.59%)
Mutual labels:  enzyme, jest
GFontsSpace
Preview: https://pankajladhar.github.io/GFontsSpace
Stars: ✭ 88 (-90.78%)
Mutual labels:  jest, snapshot-testing
React Generate Props
Generate default props based on your React component's PropTypes
Stars: ✭ 23 (-97.59%)
Mutual labels:  jest, enzyme
awesome-javascript-testing
🔧 Awesome JavaScript testing resources
Stars: ✭ 28 (-97.06%)
Mutual labels:  unit-testing, jest
egghead-bookshelf
An example React application to accompany the "Add Internationalization (i18n) to a React app using React Intl" Egghead.io course
Stars: ✭ 28 (-97.06%)
Mutual labels:  enzyme, jest

enzyme-to-json

Build Status codecov npm Version License Downloads

Convert Enzyme wrappers to a format compatible with Jest snapshot testing.

Install

$ npm install --save-dev enzyme-to-json

Usage

The 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.

In order to use the serializer, just add this line to your Jest configuration:

"snapshotSerializers": ["enzyme-to-json/serializer"]

Example

For 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.

In case you are still confused, here is a minimal example project demonstrating this configuration.

Advanced usage

Serializer in unit tests

You can add the serializer for only one Jest test file by adding these lines at the beginning of your Jest unit test file:

import {createSerializer} from 'enzyme-to-json';

expect.addSnapshotSerializer(createSerializer({mode: 'deep'}));

You can also add the serializer for all tests using the setupFilesAfterEnv configuration option from Jest.

Helper

At 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.

The 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:

import React, {Component} from 'react';
import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';

it('renders correctly', () => {
  const wrapper = shallow(
    <MyComponent className="my-component">
      <strong>Hello World!</strong>
    </MyComponent>,
  );

  expect(toJson(wrapper)).toMatchSnapshot();
});

The 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:

toJson(wrapper, {
  noKey: false,
  mode: 'deep',
});

And here are all the possible options:

Key Value Description
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.
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.
map function You can change each nested node of your component output by providing the map option. See map documentation for examples.
ignoreDefaultProps bool You can exclude the default props from snapshots in shallow mode

Docs

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].