2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
2024-07-02 03:42:40 +00:00
|
|
|
import { assertIsError } from "./is_error.ts";
|
2023-07-13 07:04:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a function which returns a promise, expecting it to reject.
|
|
|
|
*
|
2024-02-26 18:58:17 +00:00
|
|
|
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
|
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertRejects } from "@std/assert";
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw
|
|
|
|
* await assertRejects(async () => console.log("Hello world")); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @param fn The function to execute.
|
|
|
|
* @param msg The optional message to display if the assertion fails.
|
|
|
|
* @returns The promise which resolves to the thrown error.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertRejects(
|
|
|
|
fn: () => PromiseLike<unknown>,
|
|
|
|
msg?: string,
|
|
|
|
): Promise<unknown>;
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2024-02-26 18:58:17 +00:00
|
|
|
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
|
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertRejects } from "@std/assert";
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw
|
|
|
|
* await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @typeParam E The error class to assert.
|
|
|
|
* @param fn The function to execute.
|
|
|
|
* @param ErrorClass The error class to assert.
|
|
|
|
* @param msgIncludes The string that should be included in the error message.
|
|
|
|
* @param msg The optional message to display if the assertion fails.
|
|
|
|
* @returns The promise which resolves to the thrown error.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertRejects<E extends Error = Error>(
|
|
|
|
fn: () => PromiseLike<unknown>,
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-09-17 06:28:22 +00:00
|
|
|
ErrorClass: abstract new (...args: any[]) => E,
|
2023-07-13 07:04:30 +00:00
|
|
|
msgIncludes?: string,
|
|
|
|
msg?: string,
|
|
|
|
): Promise<E>;
|
|
|
|
export async function assertRejects<E extends Error = Error>(
|
|
|
|
fn: () => PromiseLike<unknown>,
|
|
|
|
errorClassOrMsg?:
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-09-17 06:28:22 +00:00
|
|
|
| (abstract new (...args: any[]) => E)
|
2023-07-13 07:04:30 +00:00
|
|
|
| string,
|
|
|
|
msgIncludesOrMsg?: string,
|
|
|
|
msg?: string,
|
|
|
|
): Promise<E | Error | unknown> {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-09-17 06:28:22 +00:00
|
|
|
let ErrorClass: (abstract new (...args: any[]) => E) | undefined;
|
|
|
|
let msgIncludes: string | undefined;
|
2023-07-13 07:04:30 +00:00
|
|
|
let err;
|
|
|
|
|
|
|
|
if (typeof errorClassOrMsg !== "string") {
|
|
|
|
if (
|
|
|
|
errorClassOrMsg === undefined ||
|
|
|
|
errorClassOrMsg.prototype instanceof Error ||
|
|
|
|
errorClassOrMsg.prototype === Error.prototype
|
|
|
|
) {
|
2024-09-17 06:28:22 +00:00
|
|
|
ErrorClass = errorClassOrMsg;
|
2023-07-13 07:04:30 +00:00
|
|
|
msgIncludes = msgIncludesOrMsg;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = errorClassOrMsg;
|
|
|
|
}
|
|
|
|
let doesThrow = false;
|
|
|
|
let isPromiseReturned = false;
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
|
|
try {
|
|
|
|
const possiblePromise = fn();
|
|
|
|
if (
|
|
|
|
possiblePromise &&
|
|
|
|
typeof possiblePromise === "object" &&
|
|
|
|
typeof possiblePromise.then === "function"
|
|
|
|
) {
|
|
|
|
isPromiseReturned = true;
|
|
|
|
await possiblePromise;
|
2024-05-07 00:08:16 +00:00
|
|
|
} else {
|
2024-08-28 22:55:24 +00:00
|
|
|
throw new Error();
|
2023-07-13 07:04:30 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (!isPromiseReturned) {
|
|
|
|
throw new AssertionError(
|
|
|
|
`Function throws when expected to reject${msgSuffix}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (ErrorClass) {
|
2024-05-07 00:08:16 +00:00
|
|
|
if (!(error instanceof Error)) {
|
2023-07-13 07:04:30 +00:00
|
|
|
throw new AssertionError(`A non-Error object was rejected${msgSuffix}`);
|
|
|
|
}
|
|
|
|
assertIsError(
|
|
|
|
error,
|
|
|
|
ErrorClass,
|
|
|
|
msgIncludes,
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err = error;
|
|
|
|
doesThrow = true;
|
|
|
|
}
|
|
|
|
if (!doesThrow) {
|
|
|
|
throw new AssertionError(
|
|
|
|
`Expected function to reject${msgSuffix}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|