• Executes a function, expecting it to throw. 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 an asynchronous function rejects, use assertRejects.

    Type Parameters

    • E extends Error = Error

    Parameters

    • fn: (() => unknown)
        • (): unknown
        • Returns unknown

    • Optional errorClassOrMsg: string | (new (...args) => E)
    • Optional msgIncludesOrMsg: string
    • Optional msg: string

    Returns E | Error | unknown

    Since

    24.7.0

    Example

    import { assertThrows } from "assert";

    assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); // Doesn't throw
    assertThrows(() => { throw new TypeError("hello world!"); }, RangeError); // Throws