Skip to main content

@std/testing@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score52%
Published2 years ago (0.215.0)

Utilities for mocking time while testing.

Classes

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

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

  • 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.

  • 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.

  • restore(): void

    Restores real time.

  • restoreFor<T>(
    callback: (...args: any[]) => Promise<T> | T,
    ...args: any[]
    ): Promise<T>

    Restores real time temporarily until callback returns and resolves.

  • 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.

  • start(): number

    The initial amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time.

  • 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.

c
TimeError(message: string)

An error related to faking time.

Interfaces

I
No documentation available
  • The frequency in milliseconds at which fake time is updated. If advanceRate is set, we will update the time every 10 milliseconds by default.

  • advanceRate: number

    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 forward at the same rate in milliseconds as real time.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/testing

Import symbol

import * as mod from "@std/testing/time";
or

Import directly with a jsr specifier

import * as mod from "jsr:@std/testing/time";