(
fn: () => PromiseLike<unknown>,
errorClassOrMsg?:
// deno-lint-ignore no-explicit-any
| (abstract new (...args: any[]) => E)
| string,
msgIncludesOrMsg?: string,
msg?: string,
)
| 54 | msg?: string, |
| 55 | ): Promise<E>; |
| 56 | export async function assertRejects<E extends Error = Error>( |
| 57 | fn: () => PromiseLike<unknown>, |
| 58 | errorClassOrMsg?: |
| 59 | // deno-lint-ignore no-explicit-any |
| 60 | | (abstract new (...args: any[]) => E) |
| 61 | | string, |
| 62 | msgIncludesOrMsg?: string, |
| 63 | msg?: string, |
| 64 | ): Promise<E | Error | unknown> { |
| 65 | // deno-lint-ignore no-explicit-any |
| 66 | let ErrorClass: (abstract new (...args: any[]) => E) | undefined; |
| 67 | let msgIncludes: string | undefined; |
| 68 | let err; |
| 69 | |
| 70 | if (typeof errorClassOrMsg !== "string") { |
| 71 | if ( |
| 72 | errorClassOrMsg === undefined || |
| 73 | errorClassOrMsg.prototype instanceof Error || |
| 74 | errorClassOrMsg.prototype === Error.prototype |
| 75 | ) { |
| 76 | ErrorClass = errorClassOrMsg; |
| 77 | msgIncludes = msgIncludesOrMsg; |
| 78 | } |
| 79 | } else { |
| 80 | msg = errorClassOrMsg; |
| 81 | } |
| 82 | let doesThrow = false; |
| 83 | let isPromiseReturned = false; |
| 84 | const msgSuffix = msg ? `: ${msg}` : "."; |
| 85 | try { |
| 86 | const possiblePromise = fn(); |
| 87 | if ( |
| 88 | possiblePromise && |
| 89 | typeof possiblePromise === "object" && |
| 90 | typeof possiblePromise.then === "function" |
| 91 | ) { |
| 92 | isPromiseReturned = true; |
| 93 | await possiblePromise; |
| 94 | } else { |
| 95 | throw new Error(); |
| 96 | } |
| 97 | } catch (error) { |
| 98 | if (!isPromiseReturned) { |
| 99 | throw new AssertionError( |
| 100 | `Function throws when expected to reject${msgSuffix}`, |
| 101 | ); |
| 102 | } |
| 103 | if (ErrorClass) { |
| 104 | if (!(error instanceof Error)) { |
| 105 | throw new AssertionError(`A non-Error object was rejected${msgSuffix}`); |
| 106 | } |
| 107 | assertIsError( |
| 108 | error, |
| 109 | ErrorClass, |
| 110 | msgIncludes, |
| 111 | msg, |
| 112 | ); |
| 113 | } |
no test coverage detected