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 KAKEHASHI Inc.
Involved in the requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.