The mock clock implementation

Implements

Constructors

  • Returns Clock

Properties

Date: {
    UTC(year: number, monthIndex: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
    now(): number;
    parse(s: string): number;
    new (...args: any[]): {
        getVarDate: (() => VarDate);
        [toPrimitive](hint: "default"): string;
        [toPrimitive](hint: "string"): string;
        [toPrimitive](hint: "number"): number;
        [toPrimitive](hint: string): string | number;
        getDate(): number;
        getDay(): number;
        getFullYear(): number;
        getHours(): number;
        getMilliseconds(): number;
        getMinutes(): number;
        getMonth(): number;
        getSeconds(): number;
        getTime(): number;
        getTimezoneOffset(): number;
        getUTCDate(): number;
        getUTCDay(): number;
        getUTCFullYear(): number;
        getUTCHours(): number;
        getUTCMilliseconds(): number;
        getUTCMinutes(): number;
        getUTCMonth(): number;
        getUTCSeconds(): number;
        setDate(date: number): number;
        setFullYear(year: number, month?: number, date?: number): number;
        setHours(hours: number, min?: number, sec?: number, ms?: number): number;
        setMilliseconds(ms: number): number;
        setMinutes(min: number, sec?: number, ms?: number): number;
        setMonth(month: number, date?: number): number;
        setSeconds(sec: number, ms?: number): number;
        setTime(time: number): number;
        setUTCDate(date: number): number;
        setUTCFullYear(year: number, month?: number, date?: number): number;
        setUTCHours(hours: number, min?: number, sec?: number, ms?: number): number;
        setUTCMilliseconds(ms: number): number;
        setUTCMinutes(min: number, sec?: number, ms?: number): number;
        setUTCMonth(month: number, date?: number): number;
        setUTCSeconds(sec: number, ms?: number): number;
        toDateString(): string;
        toISOString(): string;
        toJSON(key?: any): string;
        toLocaleDateString(): string;
        toLocaleDateString(locales?: string | string[], options?: DateTimeFormatOptions): string;
        toLocaleDateString(locales?: LocalesArgument, options?: DateTimeFormatOptions): string;
        toLocaleString(): string;
        toLocaleString(locales?: string | string[], options?: DateTimeFormatOptions): string;
        toLocaleString(locales?: LocalesArgument, options?: DateTimeFormatOptions): string;
        toLocaleTimeString(): string;
        toLocaleTimeString(locales?: string | string[], options?: DateTimeFormatOptions): string;
        toLocaleTimeString(locales?: LocalesArgument, options?: DateTimeFormatOptions): string;
        toString(): string;
        toTimeString(): string;
        toUTCString(): string;
        valueOf(): number;
    };
}
setImmediate: SetImmediate

Schedule a function to be run the next time the clock is advanced by any ammount.

setInterval: SetInterval

Schedule a function to be run when the clock advances each multiple of n past the current point.

If multiple steps are advanced at once, for example doing c.setInterval(fn, 1) ; c.advance(1000), then it will only call the function once. This allows you to simulate clock jitter.

Only ms granularity.

setTimeout: SetTimeout

Schedule a function to be run when the clock has advanced n ms beyond the current point.

Only ms granularity.

Accessors

  • get timers(): Map<number, Timer[]>
  • Returns Map<number, Timer[]>

Methods

  • Advance the clock by n ms. Use floats for smaller increments of time.

    Parameters

    • n: number

    Returns void

  • Clear an immediate timer created by the clock. (alias for Clock#clearTimeout)

    Parameters

    Returns void

  • Clear an interval created by the clock. (alias for Clock#clearTimeout)

    Parameters

    Returns void

  • Clear a timeout created by the clock.

    Parameters

    Returns void

  • Mocks all the things in the global space.

    Returns exit function, for ease of doing t.teardown(c.enter()).

    Returns (() => void)

      • (): void
      • Returns void

  • If entered, exit the clock, restoring the global state

    Returns boolean

  • Advance the clock in steps, awaiting a Promise at each step, so that actual asynchronous events can occur, as well as timers.

    Parameters

    • n: number
    • Optionalstep: number

    Returns Promise<void>

  • Mock of process.hrtime(), returning [seconds, nanoseconds] on the clock.

    Parameters

    • Optionals: [number, number]

    Returns [number, number]

  • Mock of process.hrtime.bigint(), returning BigInt representation of current nanosecond time.

    Returns bigint

  • Returns the current ms time on the clock.

    Returns number

  • The promisified setImmediate, also available via promisify(clock.setImmediate)

    Parameters

    • Optionalvalue: undefined
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns Promise<void>

  • Type Parameters

    • T = void

    Parameters

    • value: T
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns Promise<T>

  • promisified setInterval, also available via promisify(clock.setImmediate)

    Parameters

    • Optionaln: number
    • Optionalvalue: undefined
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns AsyncGenerator<void, any, unknown>

  • Type Parameters

    • T = void

    Parameters

    • Optionaln: number
    • Optionalvalue: T
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns AsyncGenerator<T, any, unknown>

  • The promisified setTimeout, also available via promisify(clock.setTimeout)

    Parameters

    • Optionaln: number
    • Optionalvalue: undefined
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns Promise<void>

  • Type Parameters

    • T = void

    Parameters

    • n: number
    • value: T
    • Optionaloptions: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optionalreffed?: boolean
      • Optionalsignal?: AbortSignal

    Returns Promise<T>

  • Set the clock to a specific time. Will fire timers that you zoom past.

    Parameters

    • to: number

    Returns void