DEV Community

Discussion on: How to mock Date with Jest

Collapse
 
avalander profile image
Avalander • Edited

I just want to point out that both Date and RealDate reference the same object, so when you replace the function Date.now, it's also changing RealDate.now and your global.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:

const realNow = Date.now

Date.now = () => 1000

Date.now = realNow
Enter fullscreen mode Exit fullscreen mode
Collapse
 
angelocicero profile image
Angelo Cicero

Very helpful!

Collapse
 
maxpou profile image
Maxence Poutord • Edited

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