Skip to main content
Module

x/mock/mod.ts>FakeTime

Utilities to help mock behavior, spy on function calls, stub methods, and fake time for tests.
Very Popular
Latest
class FakeTime
Deprecated
import { FakeTime } from "https://deno.land/x/mock@0.15.2/mod.ts";

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

Constructors

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

Properties

private
_now: number
private
_start: number
private
advanceFrequency: number
private
optional
advanceIntervalId: number
private
advanceRate: number
private
dueNodes: Map<number, DueNode>
private
dueTree: RBTree<DueNode>
private
initializedAt: number
private
timerId: Generator<number>
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

private
overrideGlobals(): void
private
restoreGlobals(): void
private
setTimer(
callback: (...args: any[]) => void,
delay?,
args: unknown[],
repeat?,
): number
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.

restore(): void

Restores time related global functions to their original state.

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.

runAllAsync(): Promise<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.

runMicrotasks(): Promise<void>

Runs all pending microtasks.

tick(ms?): void

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?): Promise<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.

Static Methods

clearInterval(id?: number): void
clearTimeout(id?: number): void
restore(): void
restoreFor<T>(callback: (...args: any[]) => Promise<T> | T, ...args: any[]): Promise<T>

Restores real time temporarily until callback returns and resolves.

setInterval(
callback: (...args: any[]) => unknown,
delay?,
...args: any[],
): number
setTimeout(
callback: (...args: any[]) => void,
delay?,
...args: any[],
): number