GithubHelp home page GithubHelp logo

isabella232 / enzyme-to-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fezvrasta/enzyme-to-json

0.0 0.0 0.0 455 KB

Snapshot test your Enzyme wrappers

License: MIT License

JavaScript 100.00%

enzyme-to-json's Introduction

enzyme-to-json

Build Status codecov Dependency Status

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 an 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 setupTestFrameworkScriptFile 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.

Docs

enzyme-to-json's People

Contributors

adriantoine avatar pedrottimark avatar stevoland avatar greenkeeperio-bot avatar prayogoa avatar difelice avatar bscharm avatar existentialism avatar daphnesmit avatar jkimbo avatar milost avatar scothis avatar truongsinh avatar jwbay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.