UNPKG

jest-time-helpers

Version:

Helpers you can use in tests that relate to the passage of time (i.e. code that involves setTimeout, setInterval, new Date(), Date.now(), etc)

51 lines (50 loc) 2.22 kB
/// <reference types="node" /> /** * Wait a number of milliseconds of _real time_ (not mocked time); useful for * allowing the runloop or external systems to advance. * * @param ts - how long to sleep for. */ export declare const sleep: (ms: number, unref?: boolean) => Promise<void> & { timeout: NodeJS.Timeout; }; /** * 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. * * @param condition - a callback function that should return true when no more sleep is required * @param maxDuration - an optional maximum duration to sleep * @param pollInterval - 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. */ export declare function sleepUntil(condition: () => boolean, maxDuration?: number, pollInterval?: 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. */ export declare function setupFakeTimers(): { setTime: (timestamp: number, increment?: number) => Promise<void>; realNow: () => number; }; /** One second in milliseconds */ export declare const SECOND = 1000; /** One minute in milliseconds */ export declare const MINUTE: number; /** One hour in milliseconds */ export declare const HOUR: number; /** One day in milliseconds */ export declare const DAY: number; /** One week in milliseconds */ export declare const WEEK: number;