Giter Site home page Giter Site logo

enzyme-to-json-bug's Introduction

Break the test

This demonstrates how a silly thing can break your tests. It uses enzyme and enzyme-to-json to implement snapshot testing with jest.

The component against which we are running tests, has a onClick handler. We test the handler by jest.fn() mock function. We simulate the click with enzyme and test to see if the mock was called.

But it also changes the values associated with jest.fn() and the snapshot gets broken.

Steps to take

Just clone this repo and yarn install followed by npx jest.

yarn install
npx jest

It should fail.

Reason?

The reason is simple. The jest.fn() is getting called before matching the snapshot. So the timestamp or basically the internal implementation and values of the jest.fn() changes everytime the tests run. Hence the snapshots will never match.

Infact, when snapshot testing, the implementation should be done in a way that internal values do not change over time.

If we try to snapshot test this object

const currentTime = Date.now();
expect(currentTime).toMatchSnapshot();

it will always fail, because currentTime changes everytime the tests are called.

Rather we can mock it

const currentTime = 1526900471489;
expect(currentTime).toMatchSnapshot();

and it will pass.

Weird thing with enzyme-to-json

For some weird reason the failing snapshots when being updated, keeps on adding to the list. I will open an issue.

enzyme-to-json-bug's People

Contributors

swashata avatar

Watchers

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