jest-mock-now
jest-mock-now copied to clipboard
Also mock the Date constructor
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?
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.
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?
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 😎
Maybe https://github.com/hustcc/jest-date-mock can help.
thanks for your link @hustcc there is also the official jest timer mocks you may be want to have a look at
It is mocker for timer, but not for time.