Function assertRejects

  • Executes a function which returns a promise, expecting it to reject.

    To assert that a synchronous function throws, use assertThrows.

    Parameters

    • fn: (() => PromiseLike<unknown>)

      The function to execute.

        • (): PromiseLike<unknown>
        • Returns PromiseLike<unknown>

    • Optionalmsg: string

      The optional message to display if the assertion fails.

    Returns Promise<unknown>

    The promise which resolves to the thrown error.

    import { assertRejects } from "@std/assert";

    await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw
    await assertRejects(async () => console.log("Hello world")); // Throws
  • Executes a function which returns a promise, expecting it to reject. If it does not, then it throws. An error class and a string that should be included in the error message can also be asserted.

    To assert that a synchronous function throws, use assertThrows.

    Type Parameters

    • E extends Error = Error

      The error class to assert.

    Parameters

    • fn: (() => PromiseLike<unknown>)

      The function to execute.

        • (): PromiseLike<unknown>
        • Returns PromiseLike<unknown>

    • ErrorClass: (new (...args: any[]) => E)

      The error class to assert.

        • new (...args): E
        • Parameters

          • Rest...args: any[]

          Returns E

    • OptionalmsgIncludes: string

      The string that should be included in the error message.

    • Optionalmsg: string

      The optional message to display if the assertion fails.

    Returns Promise<E>

    The promise which resolves to the thrown error.

    import { assertRejects } from "@std/assert";

    await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw
    await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws