2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
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.
|
|
|
|
*
|
2024-02-26 18:58:17 +00:00
|
|
|
* To assert that an asynchronous function rejects, use
|
|
|
|
* {@linkcode assertRejects}.
|
|
|
|
*
|
2023-07-13 07:04:30 +00:00
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
|
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertThrows(() => { throw new TypeError("hello world!"); }); // Doesn't throw
|
|
|
|
* assertThrows(() => console.log("hello world!")); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2024-02-26 18:58:17 +00:00
|
|
|
* To assert that an asynchronous function rejects, use
|
|
|
|
* {@linkcode assertRejects}.
|
|
|
|
*
|
2023-07-13 07:04:30 +00:00
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
|
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); // Doesn't throw
|
|
|
|
* assertThrows(() => { throw new TypeError("hello world!"); }, RangeError); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|