mmazzarolo / react-native-modal-datetime-picker

A React-Native datetime-picker for Android and iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jest snapshot fails every test run

crobinson42 opened this issue · comments

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

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?

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

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

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

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