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.

Jest snapshot fails every test run

See original GitHub issue

The default date props is different on every test run which causes the Jest snapshots to fail - any recommendation on how to handle this scenario?

First glance solution feels hacky: <DateTimePicker date={env.__TEST__ ? new Date(testDate) : undefined} />

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
crobinson42commented, Jan 18, 2019

My solution, for now, is to over-ride the Date constructor in my setupTests.js file that is ran before all tests. This is also hacky 😕 I did find the mockdate package which essentially does the same thing and it has a lot of downloads which indicates this is a popular solution.

Date = class extends Date {
  constructor(date) {
    if (date) {
        return super(date);
    }

    return new Date('2018-09-20T23:00:00Z');
  }
}
1reaction
lawrenceaglescommented, Jul 15, 2020

Adding Date.now = jest.fn(() => 1482363367071); beforeEach of my test solved this for me.

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.
Read more >
React Jest snapshot tests fail when --coverage flag enabled
I have built a React app (v16.13.1) and am testing it with Jest (v25.1.0). When I run npm test the tests all pass...
Read more >
Creating snapshots in Jest for testing React applications
It shows the different outcomes when a snapshot passes, when a snapshot test fails, and what actions take place. Snapshot testing. Setting up ......
Read more >
Devil in Disguise: Jest Snapshot Testing | by Meng Taing
Snapshot testing is a double-edge sword. It could be a great tool if you use ... After you change your code, run tests,...
Read more >
Why jest snapshots can be harmful — practical examples
toMatchSnapshot ();. Put that code in a jest test file, and a snapshot file will be created on the first run. Commit this...
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