-
Notifications
You must be signed in to change notification settings - Fork 577
/
assert_is_error_test.ts
60 lines (52 loc) · 1.85 KB
/
assert_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
// 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() 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"`,
);
});