Options
All
  • Public
  • Public/Protected
  • All
Menu

LaunchDarkly JavaScript Test Helpers (2.2.0)

Index

Type aliases

TestHttpHandler: ((req: TestHttpRequest, res: http.ServerResponse) => void) | ((req: TestHttpRequest, res: http.ServerResponse) => Promise<void>)

A function that provides a response for TestHttpServer. May be synchronous or async.

Functions

  • eventSink(emitter: EventEmitter, name: string): AsyncQueue<any>
  • Creates an AsyncQueue that will receive events. The value of each queue item is the value associated with the event, or an array if there were multiple values.

        const receivedErrors = eventSink(client, "error");
    const firstError = await receivedErrors.take();
    const secondError = await receivedErrors.take();

    Parameters

    • emitter: EventEmitter

      An event emitter.

    • name: string

      The event name.

    Returns AsyncQueue<any>

    A Promise that is resolved the first time the event is raised. It will contain the value that was provided with the event, if any-- or, if there were multiple values, an array of them.

  • failOnResolve<T>(promise: Promise<T>, timeoutMilliseconds: number, failureMessage?: string): Promise<void>
  • Causes a rejection if the given promise resolves before the timeout elapses.

    If the original promise resolves within the timeout, the new promise rejects with an UnexpectedResolveError. If the original promise rejects within the timeout, the new promise rejects with the same result. Otherwise, the new promise resolves with no value.

    Type parameters

    • T

    Parameters

    • promise: Promise<T>

      any promise

    • timeoutMilliseconds: number

      how long to wait

    • Optional failureMessage: string

      optional message that will be included in the UnexpectedResolveError

    Returns Promise<void>

    a promise that resolves only if the original promise neither resolved nor rejected

  • failOnTimeout<T>(promise: Promise<T>, timeoutMilliseconds: number, failureMessage?: string): Promise<T>
  • Causes a rejection if the timeout elapses before the given promise resolves.

    If the original promise resolves or rejects within the timeout, the new promise resolves or rejects with the same result. If the timeout elapses first, the new promise rejects with a TimeoutError.

    Test frameworks such as Jest also allow you to set an overall timeout for a test. The difference between doing that and using failOnTimeout is that failOnTimeout allows you to be more specific about which step in the test has the timing requirement, and also allows you to customize the failure message to provide better clues to what failed (since the stacktrace may not be useful in async code).

    Type parameters

    • T

    Parameters

    • promise: Promise<T>

      any promise

    • timeoutMilliseconds: number

      how long to wait

    • Optional failureMessage: string

      optional message that will be included in the TimeoutError

    Returns Promise<T>

    a promise that behaves the same as the original promise unless a timeout occurs

  • promisify(functionWithErrorAndValueCallback: (...args: any[]) => void): (...args: any[]) => Promise<any>
  • Converts a function whose last parameter is a Node-style callback (err, result) into a function with one fewer argument that returns a Promise.

    This is equivalent to util.promisify, but since Node 6 does not provide promisify, it is reimplemented here for projects that need to support Node 6. Note that it currently does not type-check the arguments.

        function someFunction(param1, param2, callback) { callback(err, result); }
    var result = await promisify(someFunction)(param1, param2);

    Parameters

    • functionWithErrorAndValueCallback: (...args: any[]) => void

      An asynchronously implemented function whose last parameter is an error-and-value callback.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns (...args: any[]) => Promise<any>

    An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with no error (null, returnValue), or rejected if it was called with an error (or if the original function threw an exception).

      • (...args: any[]): Promise<any>
      • Converts a function whose last parameter is a Node-style callback (err, result) into a function with one fewer argument that returns a Promise.

        This is equivalent to util.promisify, but since Node 6 does not provide promisify, it is reimplemented here for projects that need to support Node 6. Note that it currently does not type-check the arguments.

            function someFunction(param1, param2, callback) { callback(err, result); }
        var result = await promisify(someFunction)(param1, param2);

        Parameters

        • Rest ...args: any[]

        Returns Promise<any>

        An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with no error (null, returnValue), or rejected if it was called with an error (or if the original function threw an exception).

  • promisifySingle(functionWithSingleValueCallback: (...args: any[]) => void): (...args: any[]) => Promise<any>
  • Similar to promisify, but for functions whose callback takes only a single parameter (result) instead of the typical Node style (err, result).

        function someFunction(param1, param2, callback) { callback(result); }
    var result = await promisify(someFunction)(param1, param2);

    Parameters

    • functionWithSingleValueCallback: (...args: any[]) => void

      An asynchronously implemented function whose last parameter is a single-value callback.

        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns (...args: any[]) => Promise<any>

    An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with a value, or rejected if the original function threw an exception.

      • (...args: any[]): Promise<any>
      • Similar to promisify, but for functions whose callback takes only a single parameter (result) instead of the typical Node style (err, result).

            function someFunction(param1, param2, callback) { callback(result); }
        var result = await promisify(someFunction)(param1, param2);

        Parameters

        • Rest ...args: any[]

        Returns Promise<any>

        An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with a value, or rejected if the original function threw an exception.

  • readAllStream(stream: Stream): Promise<string>
  • Fully reads a stream.

    Parameters

    • stream: Stream

      The stream to read.

    Returns Promise<string>

    A Promise that is resolved with the full content.

  • sleepAsync(milliseconds: number): Promise<void>
  • Returns a Promise based on setTimeout().

        await sleepAsync(5000);
    

    Parameters

    • milliseconds: number

      The length of the delay.

    Returns Promise<void>

    A Promise that is resolved after the delay. It is never rejected.

  • withCloseable<T, U>(entityOrCreateFn: T | (() => T) | (() => Promise<T>), asyncCallback: (entity: T) => Promise<U>): Promise<U>
  • Ensures that an object"s close() method is called after executing a callback, even if an error occurs.

        await withCloseable(myExistingObject, async o => doSomething(o));
    await withCloseable(() => makeNewObject(), async o => doSomething(o));
    await withCloseable(async () => await makeObjectAsync(), async o => doSomething(o));

    Type parameters

    Parameters

    • entityOrCreateFn: T | (() => T) | (() => Promise<T>)

      Either the object itself, or a function (sync or async) that will create it. If this is null or undefined, you will receive a rejected Promise.

    • asyncCallback: (entity: T) => Promise<U>

      An async function to execute with the object as a parameter.

        • (entity: T): Promise<U>
        • Parameters

          • entity: T

          Returns Promise<U>

    Returns Promise<U>

    A Promise that resolves with the return value of the callback.

Generated using TypeDoc