Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/assert/mod.ts>assertThrows

The Deno Standard Library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
function assertThrows
import { assertThrows } from "https://deno.land/std@0.222.0/assert/mod.ts";

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

To assert that an asynchronous function rejects, use assertRejects.

Examples

Example 1

import { assertThrows } from "https://deno.land/std@0.222.0/assert/assert_throws.ts";

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

Parameters

fn: () => unknown
optional
msg: string

Returns

unknown

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.

Examples

Example 1

import { assertThrows } from "https://deno.land/std@0.222.0/assert/assert_throws.ts";

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

Type Parameters

optional
E extends Error = Error

Parameters

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