Options
All
  • Public
  • Public/Protected
  • All
Menu

Project jest-time-helpers

Index

Variables

Const DAY

DAY: number = ...

One day in milliseconds

Const HOUR

HOUR: number = ...

One hour in milliseconds

Const MINUTE

MINUTE: number = ...

One minute in milliseconds

Const SECOND

SECOND: 1000 = 1000

One second in milliseconds

Const WEEK

WEEK: number = ...

One week in milliseconds

Functions

setupFakeTimers

  • setupFakeTimers(): { realNow: () => number; setTime: (timestamp: number, increment?: number) => Promise<void> }
  • Sets up fake timers and Date implementation within your Jest test file. You should call this at the top of the file (not within a test or an after/before); tests that need fake timers should go into their own test file.

    Returns an object containing a setTime function which you can use to set the current (fake) date within your test to a particular JavaScript timestamp (number of milliseconds since 1970-01-01T00:00:00Z).

    Enables the jest.useFakeTimers() integration, and overwrites global.Date with a custom function that automatically applies your test file's given offset.

    Returns { realNow: () => number; setTime: (timestamp: number, increment?: number) => Promise<void> }

    • realNow: () => number
        • (): number
        • Returns the real-world current timestamp (unaffected by fake timers).

          Returns number

    • setTime: (timestamp: number, increment?: number) => Promise<void>
        • (timestamp: number, increment?: number): Promise<void>
        • Sets the offset such that a call to Date.now() (or new Date()) would return this timestamp if called immediately (but time continues to progress as expected after this). Also advances the timers by the difference from the previous offset, if positive. Setting time backwards is allowed (like setting back the system clock on a computer), but will not advance (or undo the advancement of) any timers.

          Since advancing the time a few hours might not run all the intermediary code quite right, we actually step it up by a configurable increment (defaults to one minute) at a time. Setting time backwards (no matter how far back) is done all at once.

          Parameters

          • timestamp: number

            the target timestamp

          • increment: number = ...

            the maximum we should advance time by at once in order to step towards timestamp

          Returns Promise<void>

Const sleep

  • sleep(ms: number): Promise<void>
  • Wait a number of milliseconds of real time (not mocked time); useful for allowing the runloop or external systems to advance.

    Parameters

    • ms: number

    Returns Promise<void>

sleepUntil

  • sleepUntil(condition: () => boolean, maxDuration?: number, pollInterval?: number): Promise<void>
  • Polls until the condition passes.

    Polls the condition callback every pollInterval ms of real time. If/when it returns a truthy value, resolves successfully. If maxDuration is exceeded before condition() returns true, rejects with an error.

    Parameters

    • condition: () => boolean

      a callback function that should return true when no more sleep is required

        • (): boolean
        • Returns boolean

    • maxDuration: number = 2000

      an optional maximum duration to sleep

    • pollInterval: number = 2

      an optional period of how long we should wait between checks; lower values increase load on the server but may make tests pass faster, larger values are more efficient but increase test latency.

    Returns Promise<void>

Generated using TypeDoc