@types/lolex

  • Version 5.1.6
  • Published
  • 13.8 kB
  • No dependencies
  • MIT license

Install

npm i @types/lolex
yarn add @types/lolex
pnpm add @types/lolex

Overview

TypeScript definitions for lolex

Index

Variables

variable timers

const timers: GlobalTimers<TimerId>;

    Functions

    function createClock

    createClock: <TClock extends Clock = Clock>(
    now?: number | Date,
    loopLimit?: number
    ) => TClock;
    • Creates a clock.

      Parameter now

      Current time for the clock.

      Parameter loopLimit

      Maximum number of timers that will be run when calling runAll() before assuming that we have an infinite loop and throwing an error (by default, 1000).

      Remarks

      The default epoch is 0.

    function install

    install: <TClock extends Clock = Clock>(
    opts?: LolexInstallOpts
    ) => InstalledClock<TClock>;
    • Creates a clock and installs it globally.

      Parameter now

      Current time for the clock, as with lolex.createClock().

      Parameter toFake

      Names of methods that should be faked.

    function withGlobal

    withGlobal: (global: Object) => LolexWithContext;
    • Apply new context to lolex.

      Parameter global

      New context to apply like window (in browsers) or global (in node).

    Interfaces

    interface GlobalTimers

    interface GlobalTimers<TTimerId extends TimerId> {}
    • Global methods avaliable to every clock and also as standalone methods (inside timers global object).

    property clearImmediate

    clearImmediate: (id: TTimerId) => void;
    • Clears a timer, as long as it was created using setImmediate.

      Parameter id

      Timer ID or object.

    property clearInterval

    clearInterval: (id: TTimerId) => void;
    • Clears a timer, as long as it was created using setInterval.

      Parameter id

      Timer ID or object.

    property clearTimeout

    clearTimeout: (id: TimerId) => void;
    • Clears a timer, as long as it was created using setTimeout.

      Parameter id

      Timer ID or object.

    property Date

    Date: typeof Date;
    • Implements the Date object but using this clock to provide the correct time.

    property setImmediate

    setImmediate: (callback: () => void) => TTimerId;
    • Schedules the callback to be fired once 0 milliseconds have ticked by.

      Parameter callback

      Callback to be fired.

      Remarks

      If called during a tick the callback won't fire until 1 millisecond has ticked by.

    property setInterval

    setInterval: (callback: () => void, timeout: number, ...args: any[]) => TTimerId;
    • Schedules a callback to be fired every time timeout milliseconds have ticked by.

      Parameter callback

      Callback to be fired.

      Parameter timeout

      How many ticks to wait between callbacks.

      Parameter args

      Any extra arguments to pass to the callback.

      Returns

      Time identifier for cancellation.

    property setTimeout

    setTimeout: (callback: () => void, timeout: number, ...args: any[]) => TTimerId;
    • Schedules a callback to be fired once timeout milliseconds have ticked by.

      Parameter callback

      Callback to be fired.

      Parameter timeout

      How many ticks to wait to run the callback.

      Parameter args

      Any extra arguments to pass to the callback.

      Returns

      Time identifier for cancellation.

    interface LolexClock

    interface LolexClock<TTimerId extends TimerId> extends GlobalTimers<TTimerId> {}
    • Controls the flow of time.

    property cancelAnimationFrame

    cancelAnimationFrame: (id: TTimerId) => void;
    • Cancel animation frame request.

      Parameter id

      The id returned from requestAnimationFrame method.

    property cancelIdleCallback

    cancelIdleCallback: (id: TTimerId) => void;
    • Clears a timer, as long as it was created using requestIdleCallback.

      Parameter id

      Timer ID or object.

    property countTimers

    countTimers: () => number;
    • Get the number of waiting timers.

      Returns

      number of waiting timers.

    property loopLimit

    loopLimit: number;
    • Maximum number of timers that will be run when calling runAll().

    property next

    next: () => void;
    • Advances the clock to the the moment of the first scheduled timer, firing it.

    property nextAsync

    nextAsync: () => Promise<void>;
    • Advances the clock to the the moment of the first scheduled timer, firing it.

      Also breaks the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.

    property now

    now: number;
    • Current clock time.

    property requestAnimationFrame

    requestAnimationFrame: (callback: (time: number) => void) => TTimerId;
    • Schedule callback to run in the next animation frame.

      Parameter callback

      Callback to be fired.

      Returns

      Request id.

    property requestIdleCallback

    requestIdleCallback: (callback: () => void, timeout?: number) => TTimerId;
    • Queues the callback to be fired during idle periods to perform background and low priority work on the main event loop.

      Parameter callback

      Callback to be fired.

      Parameter timeout

      The maximum number of ticks before the callback must be fired.

      Remarks

      Callbacks which have a timeout option will be fired no later than time in milliseconds.

    property reset

    reset: () => void;
    • Removes all timers and tick without firing them and restore now to its original value.

    property runAll

    runAll: () => void;
    • Runs all pending timers until there are none remaining.

      Remarks

      If new timers are added while it is executing they will be run as well.

    property runAllAsync

    runAllAsync: () => Promise<void>;
    • Runs all pending timers until there are none remaining.

      Also breaks the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.

      Remarks

      If new timers are added while it is executing they will be run as well.

    property runToFrame

    runToFrame: () => void;
    • Advanced the clock to the next animation frame while firing all scheduled callbacks.

    property runToLast

    runToLast: () => void;
    • Takes note of the last scheduled timer when it is run, and advances the clock to that time firing callbacks as necessary.

    property runToLastAsync

    runToLastAsync: () => Promise<void>;
    • Takes note of the last scheduled timer when it is run, and advances the clock to that time firing callbacks as necessary.

      Also breaks the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.

    property setSystemTime

    setSystemTime: (now?: number | Date) => void;
    • Simulates a user changing the system clock.

      Parameter now

      New system time.

      Remarks

      This affects the current time but it does not in itself cause timers to fire.

    property tick

    tick: (time: number | string) => void;
    • Advance the clock, firing callbacks if necessary.

      Parameter time

      How many ticks to advance by.

    property tickAsync

    tickAsync: (time: number | string) => Promise<void>;
    • Advance the clock, firing callbacks if necessary.

      Also breaks the event loop, allowing any scheduled promise callbacks to execute _before_ running the timers.

      Parameter time

      How many ticks to advance by.

    property timeouts

    timeouts: {};
    • Don't know what this prop is for, but it was included in the clocks that createClock or install return (it is never used in the code, for now).

    interface LolexInstallOpts

    interface LolexInstallOpts {}

      property advanceTimeDelta

      advanceTimeDelta?: number | undefined;
      • Relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change in the real system time (default: 20)

      property loopLimit

      loopLimit?: number | undefined;
      • The maximum number of timers that will be run when calling runAll() (default: 1000)

      property now

      now?: number | Date | undefined;
      • Installs lolex with the specified unix epoch (default: 0)

      property shouldAdvanceTime

      shouldAdvanceTime?: boolean | undefined;
      • Tells lolex to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by 20ms for every 20ms change in the real system time) (default: false)

      property target

      target?: any;
      • Installs lolex onto the specified target context (default: global)

      property toFake

      toFake?: FakeMethod[] | undefined;
      • An array with explicit function names to hijack. When not set, lolex will automatically fake all methods except nextTick e.g., lolex.install({ toFake: ["setTimeout", "nextTick"]}) will fake only setTimeout and nextTick

      interface LolexWithContext

      interface LolexWithContext {}

        property createClock

        createClock: <TClock extends Clock = Clock>(
        now?: number | Date,
        loopLimit?: number
        ) => TClock;

          property install

          install: <TClock extends Clock = Clock>(
          opts?: LolexInstallOpts
          ) => InstalledClock<TClock>;

            property timers

            timers: GlobalTimers<TimerId>;

              property withGlobal

              withGlobal: (global: Object) => LolexWithContext;

                interface NodeTimer

                interface NodeTimer {}
                • Timer object used in node.

                method ref

                ref: () => void;
                • Stub method call. Does nothing.

                method unref

                unref: () => void;
                • Stub method call. Does nothing.

                Type Aliases

                type BrowserClock

                type BrowserClock = LolexClock<number> & {
                /**
                * Mimics performance.now().
                */
                performance: {
                now: () => number;
                };
                };
                • Lolex clock for a browser environment.

                type Clock

                type Clock = BrowserClock | NodeClock;
                • Clock object created by lolex.

                type FakeMethod

                type FakeMethod =
                | 'setTimeout'
                | 'clearTimeout'
                | 'setImmediate'
                | 'clearImmediate'
                | 'setInterval'
                | 'clearInterval'
                | 'Date'
                | 'nextTick'
                | 'hrtime'
                | 'requestAnimationFrame'
                | 'cancelAnimationFrame'
                | 'requestIdleCallback'
                | 'cancelIdleCallback';
                • Names of clock methods that may be faked by install.

                type InstalledClock

                type InstalledClock<TClock extends Clock = Clock> = TClock & InstalledMethods;
                • Clock object created by calling install();.

                type InstalledMethods

                type InstalledMethods = {
                /**
                * Restores the original methods on the context that was passed to lolex.install,
                * or the native timers if no context was given.
                */
                uninstall: () => void;
                methods: FakeMethod[];
                };
                • Additional methods that installed clock have.

                type NodeClock

                type NodeClock = LolexClock<NodeTimer> & {
                /**
                * Mimicks process.hrtime().
                *
                * @param prevTime Previous system time to calculate time elapsed.
                * @returns High resolution real time as [seconds, nanoseconds].
                */
                hrtime(prevTime?: [number, number]): [number, number];
                /**
                * Mimics process.nextTick() explicitly dropping additional arguments.
                */
                queueMicrotask: (callback: () => void) => void;
                /**
                * Simulates process.nextTick().
                */
                nextTick: (callback: () => void) => void;
                /**
                * Run all pending microtasks scheduled with nextTick.
                */
                runMicrotasks: () => void;
                };
                • Lolex clock for a Node environment.

                type TimerId

                type TimerId = number | NodeTimer;
                • Timer identifier for clock scheduling.

                Package Files (1)

                Dependencies (0)

                No dependencies.

                Dev Dependencies (0)

                No dev dependencies.

                Peer Dependencies (0)

                No peer dependencies.

                Badge

                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/lolex.

                • Markdown
                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/lolex)
                • HTML
                  <a href="https://www.jsdocs.io/package/@types/lolex"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>