0

I have to show a message when you select a date and that date is yesterday, but only show this message when you are between the 00:00 and the 02:00 of the day of today.

I'm testing with Jest, but after tried with the jest.useFakeTimers("modern").setSystemTime(moment().startOf("day").add(3, "hour").toDate()) I have always the test passing green.

How I can mock the current time between the 00:00 and 02:00 as if the test is executed during that time?

frankfullstack
  • 498
  • 5
  • 18
  • Does this answer your question? [How to mock moment.utc() for unit tests?](https://stackoverflow.com/q/46891897/4131048) – VincenzoC Jul 08 '22 at 13:02
  • i tried without sucess, I need to emulate the jest to run the test one for after 02:00am and other before that time. – frankfullstack Jul 08 '22 at 13:29
  • I would try to separate the time from the display for testing. This way, you can pass the time into the object to have the event happen. Then, by passing different times, you can get the different events, showing the message, not showing the message. The component is then 'dumb' and simply shows the event between the times you need (which could also be passed in) and then controlling part (service, component, etc.) simply passes the current time to the component. In your test, you then simply pass different times. – Steven Scott Jul 08 '22 at 15:45
  • Does this answer your question? [Mocking moment() and moment().format using jest](https://stackoverflow.com/questions/55838798/mocking-moment-and-moment-format-using-jest) – doublethink13 Jul 08 '22 at 15:48

0 Answers0