import { DelayOptions } from "../async/delay"; /** An error related to faking time. */ export declare class TimeError extends Error { constructor(message: string); } export interface FakeTimeOptions { /** * The rate relative to real time at which fake time is updated. * By default time only moves forward through calling tick or setting now. * Set to 1 to have the fake time automatically tick foward at the same rate in milliseconds as real time. */ advanceRate: number; /** * The frequency in milliseconds at which fake time is updated. * If advanceRate is set, we will update the time every 10 milliseconds by default. */ advanceFrequency?: number; } /** * Overrides the real Date object and timer functions with fake ones that can be * controlled through the fake time instance. * * ```ts * // https://deno.land/std@$STD_VERSION/testing/mock_examples/interval_test.ts * import { * assertSpyCalls, * spy, * } from "https://deno.land/std@$STD_VERSION/testing/mock.ts"; * import { FakeTime } from "https://deno.land/std@$STD_VERSION/testing/time.ts"; * import { secondInterval } from "https://deno.land/std@$STD_VERSION/testing/mock_examples/interval.ts"; * * Deno.test("secondInterval calls callback every second and stops after being cleared", () => { * const time = new FakeTime(); * * try { * 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); * } finally { * time.restore(); * } * }); * ``` */ export declare class FakeTime { constructor(start?: number | string | Date | null, options?: FakeTimeOptions); /** Restores real time. */ static restore(): void; /** * Restores real time temporarily until callback returns and resolves. */ static restoreFor(callback: (...args: any[]) => Promise | T, ...args: any[]): Promise; /** * 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. */ get now(): number; set now(value: number); /** The initial amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time. */ get start(): number; set start(value: number); /** Resolves after the given number of milliseconds using real time. */ delay(ms: number, options?: DelayOptions): Promise; /** Runs all pending microtasks. */ runMicrotasks(): Promise; /** * 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. */ tick(ms?: number): void; /** * 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. */ tickAsync(ms?: number): Promise; /** * 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. */ next(): 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. */ nextAsync(): Promise; /** * 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. */ runAll(): void; /** * 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. */ runAllAsync(): Promise; /** Restores time related global functions to their original state. */ restore(): void; }