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