assertRejects

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

function

function assertRejects(fn: () => PromiseLike<unknown>, msg?: string): Promise<unknown>

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

To assert that a synchronous function throws, use assertThrows.

fn: () => PromiseLike<unknown>

The function to execute.

msg?: string

The optional message to display if the assertion fails.

Returns

The promise which resolves to the thrown error.

Example: Usage

import { assertRejects } from "@ende/assert";

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

function

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

To assert that a synchronous function throws, use assertThrows.

fn: () => PromiseLike<unknown>

The function to execute.

ErrorClass: new (...args: any[]) => E

The error class to assert.

msgIncludes?: string

The string that should be included in the error message.

msg?: string

The optional message to display if the assertion fails.

Returns

The promise which resolves to the thrown error.

Example: Usage

import { assertRejects } from "@ende/assert";

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

function

async function assertRejects<E extends Error = Error>(fn: () => PromiseLike<unknown>, errorClassOrMsg?: (new (...args: any[]) => E) | string, msgIncludesOrMsg?: string, msg?: string): Promise<E | Error | unknown>

Source