jest-mock-now icon indicating copy to clipboard operation
jest-mock-now copied to clipboard

Also mock the Date constructor

Open scottlepich opened this issue 7 years ago • 6 comments

Hi man, what about setting the Date constructor to return the mock date as well? Does that cause any issues that you can think of?

scottlepich avatar Jan 03 '18 23:01 scottlepich

hi @scottlepich interesting question, there shouldn't be any problem at all but why don't just use: Date.now()? is this not enough as it will always return the mocked date? I'd love to know more about your use case; really curious about this. Also feel free to create a PR if you like.

mattiaerre avatar Jan 04 '18 13:01 mattiaerre

A problem arises if a dependency uses "new Date" rather than "Date.now()". Then "now" becomes out of sync. This was causing a failing test for me. Do you think it's legit to take over the Date constructor as well in this package?

scottlepich avatar Jan 09 '18 01:01 scottlepich

Yes indeed, this is a very good point: new Date() does not use the mock. Not too sure how to accommodate this scenario in this package and also I'm not too sure if, like you said, this is the right place to do that. Let me open a PR and let's work together on that 😎

mattiaerre avatar Jan 09 '18 09:01 mattiaerre

Maybe https://github.com/hustcc/jest-date-mock can help.

hustcc avatar Jul 17 '18 14:07 hustcc

thanks for your link @hustcc there is also the official jest timer mocks you may be want to have a look at

mattiaerre avatar Jul 17 '18 15:07 mattiaerre

It is mocker for timer, but not for time.

hustcc avatar Jul 18 '18 00:07 hustcc