|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { normalizeErrorMessage } from "./errorMessage.js"; |
| 3 | + |
| 4 | +describe("normalizeErrorMessage", () => { |
| 5 | + it("extracts message from Error instances", () => { |
| 6 | + expect(normalizeErrorMessage(new Error("boom"))).toBe("boom"); |
| 7 | + }); |
| 8 | + |
| 9 | + it("passes through strings", () => { |
| 10 | + expect(normalizeErrorMessage("oops")).toBe("oops"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("extracts .message from plain objects", () => { |
| 14 | + expect(normalizeErrorMessage({ message: "hidden error" })).toBe("hidden error"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("JSON-stringifies objects without .message", () => { |
| 18 | + expect(normalizeErrorMessage({ code: 42 })).toBe('{"code":42}'); |
| 19 | + }); |
| 20 | + |
| 21 | + it("handles null", () => { |
| 22 | + expect(normalizeErrorMessage(null)).toBe("unknown error"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("handles undefined", () => { |
| 26 | + expect(normalizeErrorMessage(undefined)).toBe("unknown error"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("handles numbers", () => { |
| 30 | + expect(normalizeErrorMessage(42)).toBe("42"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("handles objects with non-string .message", () => { |
| 34 | + expect(normalizeErrorMessage({ message: 123 })).toBe('{"message":123}'); |
| 35 | + }); |
| 36 | + |
| 37 | + it("handles circular references gracefully", () => { |
| 38 | + const obj: Record<string, unknown> = {}; |
| 39 | + obj.self = obj; |
| 40 | + // Falls through JSON.stringify failure to Object.keys() |
| 41 | + expect(normalizeErrorMessage(obj)).toBe("{self}"); |
| 42 | + }); |
| 43 | +}); |
0 commit comments