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