function assertThrows
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.
Examples
Usage
Usage
import { assertThrows } from "@std/assert"; assertThrows(() => { throw new TypeError("hello world!"); }); // Doesn't throw assertThrows(() => console.log("hello world!")); // Throws
Parameters
fn: () => unknownThe function to execute.
optional
msg: stringThe optional message to display if the assertion fails.
Return Type
unknownThe error that was thrown.
assertThrows<E extends Error = Error>(): 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.
Examples
Usage
Usage
import { assertThrows } from "@std/assert"; assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); // Doesn't throw assertThrows(() => { throw new TypeError("hello world!"); }, RangeError); // Throws
Type Parameters
Parameters
Return Type
The error that was thrown.