clock-mock
    Preparing search index...

    Class Clock

    The mock clock implementation

    Implements

    Index

    Constructors

    Properties

    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

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

      Parameters

      • n: number

      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

    • 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

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

      Returns Promise<void>

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

      Type Parameters

      • T = void

      Parameters

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

      Returns Promise<T>

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

      Parameters

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

      Returns AsyncGenerator<void>

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

      Type Parameters

      • T = void

      Parameters

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

      Returns AsyncGenerator<T>

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

      Parameters

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

      Returns Promise<void>

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

      Type Parameters

      • T = void

      Parameters

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

      Returns Promise<T>

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

      Parameters

      • to: number

      Returns void