forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_error_test.ts
81 lines (70 loc) · 2.42 KB
/
is_error_test.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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { AssertionError, assertIsError, assertThrows } from "./mod.ts";
class CustomError extends Error {}
class AnotherCustomError extends Error {}
Deno.test("assertIsError() throws when given value isn't error", () => {
assertThrows(
() => assertIsError("Panic!", undefined, "Panic!"),
AssertionError,
`Expected "error" to be an Error object.`,
);
assertThrows(
() => assertIsError(null),
AssertionError,
`Expected "error" to be an Error object.`,
);
assertThrows(
() => assertIsError(undefined),
AssertionError,
`Expected "error" to be an Error object.`,
);
});
Deno.test("assertIsError() allows subclass of Error", () => {
assertIsError(new AssertionError("Fail!"), Error, "Fail!");
});
Deno.test("assertIsError() allows custom error", () => {
assertIsError(new CustomError("failed"), CustomError, "fail");
assertThrows(
() => assertIsError(new AnotherCustomError("failed"), CustomError, "fail"),
AssertionError,
'Expected error to be instance of "CustomError", but was "AnotherCustomError".',
);
});
Deno.test("assertIsError() accepts abstract class", () => {
abstract class AbstractError extends Error {}
class ConcreteError extends AbstractError {}
assertIsError(new ConcreteError("failed"), AbstractError, "fail");
});
Deno.test("assertIsError() throws with message diff containing double quotes", () => {
assertThrows(
() =>
assertIsError(
new CustomError('error with "double quotes"'),
CustomError,
'doesn\'t include "this message"',
),
AssertionError,
`Expected error message to include "doesn't include \\"this message\\"", but got "error with \\"double quotes\\"".`,
);
});
Deno.test("assertIsError() throws when given value doesn't match regex ", () => {
assertIsError(new AssertionError("Regex test"), Error, /ege/);
assertThrows(
() => assertIsError(new AssertionError("Regex test"), Error, /egg/),
Error,
`Expected error message to include /egg/, but got "Regex test"`,
);
});
Deno.test("assertIsError() throws with custom message", () => {
assertThrows(
() =>
assertIsError(
new CustomError("failed"),
AnotherCustomError,
"fail",
"CUSTOM MESSAGE",
),
AssertionError,
'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE',
);
});