forked from denoland/deno_std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_throws.ts
121 lines (119 loc) · 3.07 KB
/
assert_throws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018-2023 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.
*
* @example
* ```ts
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
*
* Deno.test("doesThrow", function (): void {
* assertThrows((): void => {
* throw new TypeError("hello world!");
* });
* });
*
* // This test will not pass.
* Deno.test("fails", function (): void {
* assertThrows((): void => {
* console.log("Hello world");
* });
* });
* ```
*/
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.
*
* @example
* ```ts
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
*
* Deno.test("doesThrow", function (): void {
* assertThrows((): void => {
* throw new TypeError("hello world!");
* }, TypeError);
* assertThrows(
* (): void => {
* throw new TypeError("hello world!");
* },
* TypeError,
* "hello",
* );
* });
*
* // This test will not pass.
* Deno.test("fails", function (): void {
* assertThrows((): void => {
* console.log("Hello world");
* });
* });
* ```
*/
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;
}