Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/testing/time.ts>FakeTime

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
class FakeTime
import { FakeTime } from "https://deno.land/std@0.220.1/testing/time.ts";

Overrides the real Date object and timer functions with fake ones that can be controlled through the fake time instance.

import {
  assertSpyCalls,
  spy,
} from "https://deno.land/std@0.220.1/testing/mock.ts";
import { FakeTime } from "https://deno.land/std@0.220.1/testing/time.ts";

function secondInterval(cb: () => void): number {
  return setInterval(cb, 1000);
}

Deno.test("secondInterval calls callback every second and stops after being cleared", () => {
  using time = new FakeTime();

  const cb = spy();
  const intervalId = secondInterval(cb);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 1);
  time.tick(3500);
  assertSpyCalls(cb, 4);

  clearInterval(intervalId);
  time.tick(1000);
  assertSpyCalls(cb, 4);
});

Constructors

new
FakeTime(start?:
| number
| string
| Date
| null
, options?: FakeTimeOptions
)

Properties

now: number

The amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time. When set, it will call any functions waiting to be called between the current and new fake time. If the timer callback throws, time will stop advancing forward beyond that timer.

start: number

The initial amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time.

Methods

delay(ms: number, options?: DelayOptions): Promise<void>

Resolves after the given number of milliseconds using real time.

next(): boolean

Advances time to when the next scheduled timer is due. If there are no pending timers, time will not be changed. Returns true when there is a scheduled timer and false when there is not.

nextAsync(): Promise<boolean>

Runs all pending microtasks then advances time to when the next scheduled timer is due. If there are no pending timers, time will not be changed.

Restores time related global functions to their original state.

Advances time forward to the next due timer until there are no pending timers remaining. If the timers create additional timers, they will be run too. If there is an interval, time will keep advancing forward until the interval is cleared.

Advances time forward to the next due timer until there are no pending timers remaining. If the timers create additional timers, they will be run too. If there is an interval, time will keep advancing forward until the interval is cleared. Runs all pending microtasks before each timer.

Runs all pending microtasks.

tick(ms?)

Adds the specified number of milliseconds to the fake time. This will call any functions waiting to be called between the current and new fake time.

Runs all pending microtasks then adds the specified number of milliseconds to the fake time. This will call any functions waiting to be called between the current and new fake time.

Static Methods

Restores real time.

restoreFor<T>(callback: (...args: any[]) => Promise<T> | T, ...args: any[]): Promise<T>

Restores real time temporarily until callback returns and resolves.