Class Clock

The mock clock implementation

Hierarchy

  • Clock

Implements

Constructors

Properties

#now: number = 1
#saved?: Saved
#timers: Map<number, Timer[]> = ...
Date: typeof MockDate
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

Methods

  • Parameters

    • f: ((...a) => any)
        • (...a): any
        • Parameters

          • Rest ...a: any[]

          Returns any

    • Rest ...a: any[]

    Returns Timer

  • Parameters

    • f: ((...a) => any)
        • (...a): any
        • Parameters

          • Rest ...a: any[]

          Returns any

    • n: number = 1
    • Rest ...a: any[]

    Returns Timer

  • Parameters

    • f: ((...a) => any)
        • (...a): any
        • Parameters

          • Rest ...a: any[]

          Returns any

    • n: number = 1
    • Rest ...a: any[]

    Returns Timer

  • 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 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
    • step: number = 5

    Returns Promise<void>

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

    Parameters

    • s: [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

    • Optional value: undefined
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns Promise<void>

  • Type Parameters

    • T = void

    Parameters

    • value: T
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns Promise<T>

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

    Parameters

    • Optional n: number
    • Optional value: undefined
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns AsyncGenerator<void, any, unknown>

  • Type Parameters

    • T = void

    Parameters

    • Optional n: number
    • Optional value: T
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns AsyncGenerator<T, any, unknown>

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

    Parameters

    • Optional n: number
    • Optional value: undefined
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns Promise<void>

  • Type Parameters

    • T = void

    Parameters

    • n: number
    • value: T
    • Optional options: {
          reffed?: boolean;
          signal?: AbortSignal;
      }
      • Optional reffed?: boolean
      • Optional signal?: AbortSignal

    Returns Promise<T>

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

    Parameters

    • to: number

    Returns void

Generated using TypeDoc