-
Notifications
You must be signed in to change notification settings - Fork 0
/
berror_test.ts
48 lines (40 loc) · 1.33 KB
/
berror_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
import { BError } from "./berror.ts";
import { assertEquals, assertStringContains } from "https://deno.land/std/testing/asserts.ts";
Deno.test("onlyMesesage", function () {
const e = new BError("A");
assertEquals(e.message, "A");
});
Deno.test("messageAndCause", function () {
const e = new BError("A", Error("B"));
assertEquals(e.message, "A: B");
});
Deno.test("full", function () {
const e = new BError("A", Error("B"), { foo: "bar" });
assertEquals(e.message, "A: B");
});
Deno.test("simpleChain", function () {
const e = new BError("A", new BError("B", new Error("C")));
assertEquals(e.message, "A: B: C");
});
Deno.test("metadata", function () {
const e1 = new BError("A", undefined, { foo: "bar", age: 42 });
const e2 = new BError("A", e1, { foo: "baz", name: "Deno" });
assertEquals(e2.metadata, { foo: "baz", age: 42, name: "Deno" });
});
Deno.test("nonObject", function () {
const e = new BError("A", ({ foo: "bar", age: 42 }) as any);
assertEquals(
e.message,
`A: non-error object thrown: {"foo":"bar","age":42}`
);
});
const a = () => {throw Error("original error")}
const b = () => a()
const c = () => {
try {b()} catch (e) { throw new BError('could not run b', e)}
}
Deno.test("has original stack", function () {
try {c()} catch (e) {
assertStringContains(e.stack, "original error");
}
});