Sometimes, we want to set a mock date in Jest.
In this article, we’ll look at how to set a mock date in Jest.
How to set a mock date in Jest?
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.
Conclusion
To set a mock date in Jest, we can use the useFakeTimers
and setSysttemTime
methods.