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 assertRejects
assertRejects(
fn: () => PromiseLike<unknown>,
msg?: string
): Promise<unknown>

Executes a function which returns a promise, expecting it to reject.

Examples

Example 1

import { assertRejects } from "@std/assert/assert_rejects";

await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw
await assertRejects(async () => console.log("Hello world")); // Throws

Parameters

fn: () => PromiseLike<unknown>
optional
msg: string

Return Type

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

Executes a function which returns a promise, expecting it to reject. 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 { assertRejects } from "@std/assert/assert_rejects";

await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw
await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws

Type Parameters

E extends Error = Error

Parameters

fn: () => PromiseLike<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 { assertRejects } from "@std/assert/assert_rejects";
or

Import directly with a jsr specifier

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