This release is 28 versions behind 1.0.16 — the latest version of @std/assert. Jump to latest
Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Common assertion functions, especially useful for testing
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
2 years ago (0.219.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertIsError } from "./assert_is_error.ts"; import { AssertionError } from "./assertion_error.ts"; /** * Executes a function, expecting it to throw. If it does not, then it * throws. * * To assert that an asynchronous function rejects, use * {@linkcode assertRejects}. * * @example * ```ts * import { assertThrows } from "@std/assert/assert_throws"; * * assertThrows(() => { throw new TypeError("hello world!"); }); // Doesn't throw * assertThrows(() => console.log("hello world!")); // Throws * ``` */ export function assertThrows( fn: () => unknown, msg?: string, ): 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 * {@linkcode assertRejects}. * * @example * ```ts * 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 * ``` */ export function assertThrows<E extends Error = Error>( fn: () => unknown, // deno-lint-ignore no-explicit-any ErrorClass: new (...args: any[]) => E, msgIncludes?: string, msg?: string, ): E; export function assertThrows<E extends Error = Error>( fn: () => unknown, errorClassOrMsg?: // deno-lint-ignore no-explicit-any | (new (...args: any[]) => E) | string, msgIncludesOrMsg?: string, msg?: string, ): E | Error | unknown { // deno-lint-ignore no-explicit-any let ErrorClass: (new (...args: any[]) => E) | undefined = undefined; let msgIncludes: string | undefined = undefined; let err; if (typeof errorClassOrMsg !== "string") { if ( errorClassOrMsg === undefined || errorClassOrMsg.prototype instanceof Error || errorClassOrMsg.prototype === Error.prototype ) { // deno-lint-ignore no-explicit-any ErrorClass = errorClassOrMsg as new (...args: any[]) => E; msgIncludes = msgIncludesOrMsg; } else { msg = msgIncludesOrMsg; } } else { msg = errorClassOrMsg; } let doesThrow = false; const msgSuffix = msg ? `: ${msg}` : "."; try { fn(); } catch (error) { if (ErrorClass) { if (error instanceof Error === false) { throw new AssertionError(`A non-Error object was thrown${msgSuffix}`); } assertIsError( error, ErrorClass, msgIncludes, msg, ); } err = error; doesThrow = true; } if (!doesThrow) { msg = `Expected function to throw${msgSuffix}`; throw new AssertionError(msg); } return err; }