1

I want to change how Node sees what the current time is without changing any system settings. For instance: If the current year is 2021, I want Node to think the current year is 2022 without changing system date nor timezones.

How would I achieve this and is this even possible?

I want to do this to test if my application will be working as intended in the future, since it uses the current date for a lot of functionalities.

Float07
  • 314
  • 2
  • 12
  • 1
    Could you maybe elaborate why you want to achieve this specific change in NodeJS time, maybe there's a different way to tackle your root problem? And what have you tried so far? – marpme Dec 01 '21 at 12:05
  • Sure! I want to test if some functionalities will work fine when we get to 2022. I thought about changing the server's system date but I don't think that's possible. Apart from that I couldn't think of anything else to try. – Float07 Dec 01 '21 at 12:07
  • 1
    Why not just change that date to sometime in 2022? – goto Dec 01 '21 at 12:09
  • It's not just a date. The current date is used a lot of times throughout the code, so I'd have to change a lot of lines of code for that. – Float07 Dec 01 '21 at 12:11
  • 1
    Perhaps in your tests you could just mock the date? https://stackoverflow.com/questions/28504545/how-to-mock-a-constructor-like-new-date – goto Dec 01 '21 at 12:12
  • 2
    How about moving the date as a parameter and then pass modified parameter in your test. Or secondly, packing the `new Date()` into a simple function like `getCurrentDate` and then stub `getCurrentDate` within your test? Do you see this as possible solution? – marpme Dec 01 '21 at 12:13
  • 2
    I think creating a more generalized, reused function sounds also well when hearing this: _"the current date is used a lot of times throughout the code"_ – marpme Dec 01 '21 at 12:14
  • @goto1 solution looks promising. I'd have to test if this works but if I understand well this could allow my Node server to "think" the current date is a different one, so I'd not have to change a lot of my code. – Float07 Dec 01 '21 at 12:29
  • I also liked @marpme solution about creating a wrapper function to get the current date. When testing I could just change this function. But the problem is I'd have to change a lot of code so this new function would be called. – Float07 Dec 01 '21 at 12:30
  • Why would you want to change your system date? The Date object would still be the same even if you change the timezone. It depends on unix epoch, not time itself. – Palm Dec 01 '21 at 12:32
  • @Palm sorry if I wasn't clear enough. I want Node to think the date is something other than the current (i.e. make it think that we are actually in 2022) . I don't want to change the time zone nor the system date. – Float07 Dec 01 '21 at 12:37
  • I don't think you can do that, let's see if anyone can help. – Palm Dec 01 '21 at 14:42
  • update on @goto1 solution. It looks like Jest's mock functions aren't suitable for what I was looking for at the moment. By what I learnt, I can't build and run my app with Jest's mock functions, I can just use the mock functions inside tests environments. – Float07 Dec 02 '21 at 14:32

0 Answers0