1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[備忘録] jestで現在時刻をmockする

Posted at

TL;DR

describe('timer mock', () => {
  beforeEach(() => {
    jest.useFakeTimers()
  })

  it('test', () => {
    jest.setSystemTime(new Date('2023-02-01T12:05:13.354+09:00'))
    // TODO assert
  })

  afterEach(() => {
    jest.useRealTimers()
  })
})

useFakeTimersしないと...

下記がターミナルに表示され、現在時刻はmockできない。

A function to advance timers was called but the timers APIs are not replaced with fake timers. Call `jest.useFakeTimers()` in this test file or enable fake timers for all tests by setting 'fakeTimers': {'enableGlobally': true} in Jest configuration file.
    Stack Trace:

Reference

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?