Fake Timers in Jest

From the Jest cheat sheet · Timers & Dates · verified Jul 2026

Fake Timers

Controlling time in tests with Jest fake timers

javascript
// Enable fake timers
beforeEach(() => {
  jest.useFakeTimers()
})

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

test('timer test', () => {
  const callback = jest.fn()

  setTimeout(callback, 1000)

  // Fast-forward time
  jest.advanceTimersByTime(1000)
  expect(callback).toHaveBeenCalledTimes(1)
})

// Run all timers
jest.runAllTimers()

// Run only pending timers
jest.runOnlyPendingTimers()
💡 Modern timers (default) also mock Date, legacy timers don't
⚡ Use runOnlyPendingTimers to avoid infinite timer loops
📌 Always restore real timers in afterEach to avoid test pollution
🟢 setSystemTime allows testing date-dependent logic easily
timersdatemocking
Back to the full Jest cheat sheet