To set a mock date in Jest, we can use the useFakeTimers
and setSysttemTime
methods.
For instance, we write:
jest
.useFakeTimers()
.setSystemTime(new Date('2022-01-01').getTime());
to call useFakeTimers
to let us mock timers.
Then we call setSystemTime
to set the time of the mock timer by calling it with a timestamp in milliseconds.