Jest snapshot fails every test run
See original GitHub issueThe 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:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My solution, for now, is to over-ride the
Date
constructor in mysetupTests.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.Adding
Date.now = jest.fn(() => 1482363367071);
beforeEach of my test solved this for me.