plaa / TimeShift-js

Library for mocking JavaScript's Date object
MIT License
65 stars 28 forks source link

Allow the date to progress at the natural rate rather than returning the same date each time #13

Closed MaxboDev closed 2 years ago

MaxboDev commented 2 years ago

I've used timeshift-js in my app to enable charts to always be rendered the same so we can do visual regression tests. Its been really useful so thanks for developing this library.

I did encounter a problem at first though, which was that once I mocked the date, all of the chart animations stopped working. After a little debugging I realised that this was because after mocking the date with setTime(), the same date was returned every time. I was able to overcome this by doing something like the following to increment the time ever 100ms.

setInterval(() => {
  setTime(new Date().getTime() + 100);
}, 100);

This worked for my purposes but it would have been neater if I could just set an option on timeshift that allows time to progress as normal starting from the time I set. Another option would have been to allow setTime() to be passed a callback, which would make it easier to return a different time on each call.

I would be happy to contribute this but wanted to make sure it was something that you'd like to implement before attempting any changes.

Thanks!

plaa commented 2 years ago

Hi @M4sterShake !

An option to allow time to progress normally sounds like a rather strange and specific use-case when the point of the library is to make the time exact and predictable. The option for allowing a callback to be passed to setTime sounds more reasonable and deterministic, and potentially has other uses as well.

If you're interested in implementing that I'm willing to release it. The example of allowing time to flow starting from a specific point in time could be added as an example in the README, it shouldn't be many lines of code.

MaxboDev commented 2 years ago

Hi @plaa,

I agree its fairly specific for the most part. It's just the fact that it broke canvas animations when set to a static value that made me think others may have similar problems. Also agree that allowing users to pass a callback will give the user more flexibility to do what they want.

Will see if I can work on it this weekend.

MaxboDev commented 2 years ago

Hi All, any chance of a review on my PR for this? https://github.com/plaa/TimeShift-js/pull/14 thanks :)

plaa commented 2 years ago

Closed by #14