assertThrows
import { assertThrows } from "jsr:@ende/assert";function
function assertThrows(fn: () => unknown, msg?: string): unknownExecutes a function, expecting it to throw. If it does not, then it throws.
To assert that an asynchronous function rejects, use
assertRejects.
fn: () => unknownThe function to execute.
msg?: stringThe 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): EExecutes 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: () => unknownThe function to execute.
ErrorClass: new (...args: any[]) => EThe error class to assert.
msgIncludes?: stringThe string that should be included in the error message.
msg?: stringThe 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