Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest snapshot fails every test run #216

Closed
crobinson42 opened this issue Jan 18, 2019 · 6 comments
Closed

Jest snapshot fails every test run #216

crobinson42 opened this issue Jan 18, 2019 · 6 comments

Comments

@crobinson42
Copy link

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} />

@crobinson42
Copy link
Author

screenshot 2019-01-18 08 59 05

@mmazzarolo
Copy link
Owner

Hi @crobinson42 ouch, you're right, it does feel hacky but I'm not aware of other workarounds without making breaking changes (other than creating weird props like useDefaultInitialDate={false}).

Do you have any ideas?

@crobinson42
Copy link
Author

crobinson42 commented 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');
  }
}

@mmazzarolo
Copy link
Owner

Gotcha, thanks for answering. If you find any other workarounds feel free to add them here (I'm adding this issue to the README.md).

@danielst19
Copy link

Hi @mmazzarolo, How do I deactivate weekends in the datepicker? I appreciate that you can help me

@ghost
Copy link

ghost commented Jul 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants