question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make snapshot tests work

See original GitHub issue

I’m using Jest for testing and I try to make snapshot tests of components rendering a DayPickerRangeController (although I guess this issue would be the same for other date pickers).

Say I want to make a snapshot test of a simple component wrapping DayPickerRangeController; used for setting nice defaults for instance, in my example numberOfMonths, startDateOffset and endDateOffset.

import React from "react";
import moment from "moment";
import PropTypes from "prop-types";
import momentPropTypes from "react-moment-proptypes";
import "react-dates/initialize";
import { START_DATE, END_DATE } from "react-dates/constants";
import { DayPickerRangeController } from "react-dates";

const DayPickerRangeControllerWrapper = ({
  startDate,
  endDate,
  focusedInput,
  onDatesChange,
  onFocusChange
}) => (
  <DayPickerRangeController
    startDate={startDate}
    endDate={endDate}
    onDatesChange={onDatesChange}
    focusedInput={focusedInput}
    onFocusChange={onFocusChange}
    numberOfMonths={1}
    startDateOffset={day => day.clone().add(1, "day")}
    endDateOffset={day => day.clone().subtract(1, "day")}
  />
);

DayPickerRangeControllerWrapper.propTypes = {
  startDate: momentPropTypes.momentObj,
  endDate: momentPropTypes.momentObj,
  focusedInput: PropTypes.oneOf([START_DATE, END_DATE]),
  onDatesChange: PropTypes.func.isRequired,
  onFocusChange: PropTypes.func.isRequired
};

DayPickerRangeControllerWrapper.defaultProps = {
  startDate: moment("2018-01-01"),
  endDate: moment("2018-01-07"),
  focusedInput: START_DATE
};

export default DayPickerRangeControllerWrapper;

I then try to snapshot test it

import React from "react";
import renderer from "react-test-renderer";
import DayPickerRangeControllerWrapper from "./DayPickerRangeControllerWrapper";

describe("DayPickerRangeControllerWrapper", () => {
  it("renders correctly", () => {
    const tree = renderer
      .create(
        <DayPickerRangeControllerWrapper
          onDatesChange={() => {}}
          onFocusChange={() => {}}
          onClearDates={() => {}}
        />
      )
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
});

I get the following error

TypeError: Cannot read property '__consolidated_events_handlers__' of null

      at addEventListener (node_modules/consolidated-events/lib/index.js:22:14)
      at CalendarMonthGrid.componentDidMount (node_modules/react-dates/lib/components/CalendarMonthGrid.js:215:77)

This seems to be somehow related to https://github.com/airbnb/react-dates/issues/528, but I haven’t found a way to properly (and simply) mock ReactDOM or CalendarMonthGrid (that doesn’t seem like a good way to go though). I could always unit test my wrapper component, but I would like to be able to snapshot test it as well.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolasrouannecommented, Sep 20, 2018

I think I found the right way to do it. What I am really trying to test here is my DayPickerRangeControllerWrapper where I have some custom logic (pretty simple in my example); I don’t want to test react-dates’s DayPickerRangeController, as it’s not my concern.

So I need to mock DayPickerRangeController, which is a named export of the react-dates module (mock modules documentation by Jest).

react-dates mock

only mocking DayPickerRangeController since it’s the one I’m using

// ./__mocks__/react-dates.js
const DayPickerRangeController = ({ ...props }) =>
  `<DayPickerRangeController" ${JSON.stringify(props)}/>`;

export { DayPickerRangeController };

Snapshot

It works with the resulting snapshot

I chose to display props passed to my wrapper component in the snapshot, since I consider it’s part of my wrapper component’s contract to pass down the props to my wrapped component

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DayPickerRangeControllerWrapper renders correctly 1`] = `"<DayPickerRangeController\\" {\\"startDate\\":\\"2018-03-02T23:00:00.000Z\\",\\"endDate\\":\\"2018-01-06T23:00:00.000Z\\",\\"focusedInput\\":\\"startDate\\",\\"numberOfMonths\\":1}/>"`;
0reactions
shunfancommented, Dec 20, 2018

@nicolasrouanne

You could mock out the DayPickerRangeController using Jest:

jest.mock('react-dates', () => ({
  DayPickerRangeController: 'mockDayPickerRangeController',
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly. A typical snapshot test...
Read more >
Creating snapshots in Jest for testing React applications
To better understand snapshots, open the elements section in the tab where your React application is running. Compare it side by side with...
Read more >
How To Write Snapshot Tests For React Components With Jest
When writing snapshot tests for a React component, you first need to have code in a working state. Then, generate a snapshot of...
Read more >
Snapshot Testing: Benefits and Drawbacks - SitePen
Snapshot testing is a type of “output comparison” or “golden master” testing. These tests prevent regressions by comparing the current ...
Read more >
What is a snapshot test? - Lara Schenck
A snapshot test is a specific kind of technique for regression testing, that is, tests that make sure changes to a code-base do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found