@kayahr/assert
    Preparing search index...

    Function assertThrowWithMessage

    Asserts that given function (synchronous or asynchronous) does throw an exception with the given type and message. When function is asynchronous then assertThrowWithMessage returns a Promise which must be awaited in order to complete the assertion.

    function to test

    The expected error type the function is expected to throw.

    The expected error message the function is expected to throw. Can be a string or a regular expression.

    Optional reason added to exception message when assertion fails.

    Examples:

    assertThrowWithMessage(() => someSyncFunc(), RangeError, "Value must be between 2 and 5");
    await assertThrowWithMessage(() => someAsyncFunc(), RangeError, "Value must be between 2 and 5");
    • Asserts that given asynchronous function does reject the returned promise.

      Parameters

      • fn: () => Promise<unknown>

        The function to call.

      • type: Function

        The expected error type the function is expected to throw.

      • message: string | RegExp

        The expected error message the function is expected to throw. Can be a string or a regular expression.

      • Optionalreason: string

        Optional reason added to exception message when assertion fails.

      Returns Promise<void>

      Promise which must be awaited in order to complete assertion.

      AssertionError when function throws exception not matching the given one (or does not throw anything when no error specified).

    • Asserts that given synchronous function does reject the returned promise.

      Parameters

      • fn: () => unknown

        The function to call.

      • type: Function

        The expected error type the function is expected to throw.

      • message: string | RegExp

        The expected error message the function is expected to throw. Can be a string or a regular expression.

      • Optionalreason: string

        Optional reason added to exception message when assertion fails.

      Returns void

      AssertionError when function throws exception not matching the given one (or does not throw anything when no error specified).