{"record":{"id":"1d959c71d8ad6c92","repo":"vitest-dev/vitest","slug":"a-function-to-advance-timers-was-called-but-the-ti","errorCode":null,"errorMessage":"A function to advance timers was called but the timers APIs are not mocked. Call `vi.useFakeTimers()` in the test file first.","messagePattern":"A function to advance timers was called but the timers APIs are not mocked\\. Call `vi\\.useFakeTimers\\(\\)` in the test file first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/mock/timers.ts","lineNumber":260,"sourceCode":"        this._clock.setTickMode({ mode: 'interval', delta: interval })\n      }\n      else {\n        throw new Error(`Invalid tick mode: ${mode}`)\n      }\n    }\n  }\n\n  configure(config: FakeTimersConfig): void {\n    this._userConfig = config\n  }\n\n  isFakeTimers(): boolean {\n    return this._fakingTime\n  }\n\n  private _checkFakeTimers() {\n    if (!this._fakingTime) {\n      throw new Error(\n        'A function to advance timers was called but the timers APIs are not mocked. '\n        + 'Call `vi.useFakeTimers()` in the test file first.',\n      )\n    }\n\n    return this._fakingTime\n  }\n}\n","sourceCodeStart":242,"sourceCodeEnd":269,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/mock/timers.ts#L242-L269","documentation":"Thrown by `FakeTimers._checkFakeTimers()` (and therefore by every timer-advancing API such as `runAllTimers`, `advanceTimersByTime`, `runOnlyPendingTimers`) when `_fakingTime` is false — i.e. `vi.useFakeTimers()` was never called (or was reset). The message directs the developer to enable fake timers first.","triggerScenarios":"Calling `vi.runAllTimers()` / `vi.advanceTimersByTime(1000)` / `vi.advanceTimersToNextTimer()` without first calling `vi.useFakeTimers()`; calling after `vi.useRealTimers()`; calling before `vi.useFakeTimers()` in a test that imported a module using real timers.","commonSituations":"Forgetting the `vi.useFakeTimers()` call; helper utilities that advance timers but assume the test set them up; shared `beforeEach` that conditionally enables fake timers; calling `vi.useRealTimers()` mid-test then advancing.","solutions":["Add `vi.useFakeTimers()` at the top of the test (or in `beforeEach`) before any `vi.advanceTimers*`/`runAllTimers` call.","If you previously called `vi.useRealTimers()`, re-enable fakes with `vi.useFakeTimers()` before advancing.","Guard shared helpers with `if (vi.isFakeTimersActive?.()) { ... }` or accept that fake timers must be set up by the caller."],"exampleFix":"// before\nit('flushes', () => {\n  vi.advanceTimersByTime(1000)\n})\n\n// after\nit('flushes', () => {\n  vi.useFakeTimers()\n  vi.advanceTimersByTime(1000)\n})","handlingStrategy":"validation","validationCode":"function advanceSafely(ms) {\n  if (!vi.isFakeTimersActive()) {\n    vi.useFakeTimers()\n  }\n  vi.advanceTimersByTime(ms)\n}","typeGuard":"function fakeTimersActive() {\n  return typeof vi.isFakeTimersActive === 'function' && vi.isFakeTimersActive()\n}","tryCatchPattern":null,"preventionTips":["Call vi.useFakeTimers() in beforeEach for suites that advance timers.","Guard shared timer helpers with vi.isFakeTimersActive() before advancing.","Avoid mixing vi.useRealTimers() mid-test without re-enabling fakes."],"tags":["fake-timers","usefaketimers","ordering","api-misuse"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}