Skip to main content

@std/assert@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function assertThrows
assertThrows(
fn: () => unknown,
msg?: string
): unknown

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

Examples

Example 1

import { assertThrows } from "@std/assert/assert_throws";

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

Parameters

fn: () => unknown
optional
msg: string

Return Type

unknown
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.

Examples

Example 1

import { assertThrows } from "@std/assert/assert_throws";

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

Type Parameters

E extends Error = Error

Parameters

fn: () => unknown
ErrorClass: new (...args: any[]) => E
optional
msgIncludes: string
optional
msg: string

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/assert

Import symbol

import { assertThrows } from "@std/assert";
or

Import directly with a jsr specifier

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