assertThrows

import { assertThrows } from "jsr:@ende/assert";

function

function assertThrows(fn: () => unknown, msg?: string): unknown

Executes a function, expecting it to throw. If it does not, then it throws.

To assert that an asynchronous function rejects, use assertRejects.

fn: () => unknown

The function to execute.

msg?: string

The optional message to display if the assertion fails.

Returns

The error that was thrown.

Example: Usage

import { assertThrows } from "@ende/assert";

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

function

function assertThrows<E extends Error = Error>(fn: () => unknown, ErrorClass: new (...args: any[]) => E, msgIncludes?: string, msg?: string): E

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.

fn: () => unknown

The function to execute.

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

The error class to assert.

msgIncludes?: string

The string that should be included in the error message.

msg?: string

The optional message to display if the assertion fails.

Returns

The error that was thrown.

Example: Usage

import { assertThrows } from "@ende/assert";

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

function

function assertThrows<E extends Error = Error>(fn: () => unknown, errorClassOrMsg?: (new (...args: any[]) => E) | string, msgIncludesOrMsg?: string, msg?: string): E | Error | unknown

Source