forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorMessage.test.ts
More file actions
58 lines (48 loc) · 1.97 KB
/
Copy patherrorMessage.test.ts
File metadata and controls
58 lines (48 loc) · 1.97 KB
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
import { describe, expect, it } from "vitest";
import { normalizeErrorMessage } from "./errorMessage.js";
describe("normalizeErrorMessage", () => {
it("returns the message of an Error instance", () => {
expect(normalizeErrorMessage(new Error("render failed"))).toBe("render failed");
});
it("returns the message of an Error subclass", () => {
expect(normalizeErrorMessage(new TypeError("bad input"))).toBe("bad input");
});
it("passes a plain string through", () => {
expect(normalizeErrorMessage("already a message")).toBe("already a message");
});
it("prefers a string message property on plain objects", () => {
expect(normalizeErrorMessage({ message: "from object", code: 500 })).toBe("from object");
});
it("stringifies plain objects without a message property", () => {
expect(normalizeErrorMessage({ status: 503, retryable: true })).toBe(
'{"status":503,"retryable":true}',
);
});
it("stringifies objects whose message property is not a string", () => {
expect(normalizeErrorMessage({ message: 42 })).toBe('{"message":42}');
});
it("falls back to a key list when JSON.stringify throws on a cycle", () => {
const cyclic: Record<string, unknown> = { code: "E_LOOP" };
cyclic["self"] = cyclic;
expect(normalizeErrorMessage(cyclic)).toBe("{code, self}");
});
it("falls back to String() when an object resists both JSON and key listing", () => {
const hostile = new Proxy(
{},
{
ownKeys() {
throw new Error("no keys for you");
},
},
);
expect(normalizeErrorMessage(hostile)).toBe("[object Object]");
});
it("returns 'unknown error' for null and undefined", () => {
expect(normalizeErrorMessage(null)).toBe("unknown error");
expect(normalizeErrorMessage(undefined)).toBe("unknown error");
});
it("stringifies non-object primitives", () => {
expect(normalizeErrorMessage(42)).toBe("42");
expect(normalizeErrorMessage(false)).toBe("false");
});
});