I spent too many hours trying to mock correctly the JavaScript's Date object. I tried a few things I've found on Google... without success. I final...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for sharing this--very helpful.
The approach described here worked well for me, but then I stumbled onto another option that uses Jest's own API (I don't believe was available when this post was originally written). You can now do something like this:
Unfortunately
jest.useFakeTimers
seems to not work well with native Promises, which means you can't use it in anasync
call.github.com/facebook/jest/issues/10221
Thank you for this!
Thanks! That's true, it was added last may with Jest 26 :)
jestjs.io/blog/2020/05/05/jest-26
setSystemTime is giving error- setSystemTime does not exist on jest
I just want to point out that both
Date
andRealDate
reference the same object, so when you replace the functionDate.now
, it's also changingRealDate.now
and yourglobal.Date = RealDate
at the end is not doing what you think it's doing.What you should do if you want to keep and reassign the original
now
function is keep a reference to that function:Very helpful!
🙊Oh! You're completely right! Thank you so much, I will update my post!!! :)
(sorry for the late answer, I didn't saw the notification...)
Great! Just what I was looking for!
Thanks for the
process.env.TZ = 'GMT'
tip 🙏🏽Great job!
Thank you a lot, it really helped.