Tip: Mocking JavaScript Date Class with Jest

Tip: Mocking JavaScript Date Class with Jest

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
1 min read
Jest

I was looking for a technique to mock new Date() with Jest. Developers can use fake timers and setSystemTime like the following.

jest.useFakeTimers().setSystemTime(new Date('2023-01-02 12:34:56'));
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at iret, Inc.
Architecting and developing cloud native applications mainly with AWS. Japan AWS Top Engineers 2020-2023