|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { Payload } from "../index"; |
| 3 | + |
| 4 | +// Mock external dependencies that can't be resolved by Vite in test env |
| 5 | +vi.mock("promjs", () => { |
| 6 | + const mockCounter = { inc: vi.fn() }; |
| 7 | + const mockRegistry = { |
| 8 | + create: vi.fn(() => mockCounter), |
| 9 | + metrics: vi.fn(() => ""), |
| 10 | + }; |
| 11 | + return { default: () => mockRegistry }; |
| 12 | +}); |
| 13 | + |
| 14 | +vi.mock("toucan-js", () => { |
| 15 | + return { |
| 16 | + Toucan: vi.fn().mockImplementation(() => ({ |
| 17 | + captureException: vi.fn(), |
| 18 | + })), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +describe("handlePrettyErrorRequest", () => { |
| 23 | + it("should propagate async rejections to callers", async () => { |
| 24 | + // Mock Youch to throw asynchronously |
| 25 | + vi.doMock("../Youch", () => { |
| 26 | + return { |
| 27 | + default: vi.fn().mockImplementation(() => ({ |
| 28 | + addLink: vi.fn(), |
| 29 | + toHTML: vi.fn().mockRejectedValue(new Error("Youch async error")), |
| 30 | + })), |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + // handlePrettyErrorRequest is async — if it's not awaited, async |
| 35 | + // rejections (e.g. from Youch.toHTML()) won't be caught by a |
| 36 | + // surrounding try/catch, leading to unhandled promise rejections. |
| 37 | + // This test verifies that the function properly rejects so that |
| 38 | + // callers who `await` it can catch the error. |
| 39 | + const { handlePrettyErrorRequest } = await import("../index"); |
| 40 | + |
| 41 | + const payload: Payload = { |
| 42 | + url: "https://example.com", |
| 43 | + method: "GET", |
| 44 | + headers: { "content-type": "text/html" }, |
| 45 | + error: { |
| 46 | + message: "Test error", |
| 47 | + name: "Error", |
| 48 | + stack: "Error: Test error\n at test:1:1", |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + await expect(handlePrettyErrorRequest(payload)).rejects.toThrow( |
| 53 | + "Youch async error" |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("reviveError", () => { |
| 59 | + it("should revive a plain Error", async () => { |
| 60 | + const { reviveError } = await import("../index"); |
| 61 | + const error = reviveError({ |
| 62 | + message: "test", |
| 63 | + name: "Error", |
| 64 | + stack: "Error: test\n at foo:1:1", |
| 65 | + }); |
| 66 | + expect(error).toBeInstanceOf(Error); |
| 67 | + expect(error.message).toBe("test"); |
| 68 | + expect(error.name).toBe("Error"); |
| 69 | + expect(error.stack).toBe("Error: test\n at foo:1:1"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should revive a TypeError", async () => { |
| 73 | + const { reviveError } = await import("../index"); |
| 74 | + const error = reviveError({ |
| 75 | + message: "x is not a function", |
| 76 | + name: "TypeError", |
| 77 | + }); |
| 78 | + expect(error).toBeInstanceOf(TypeError); |
| 79 | + expect(error.message).toBe("x is not a function"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should revive an error with a cause", async () => { |
| 83 | + const { reviveError } = await import("../index"); |
| 84 | + const error = reviveError({ |
| 85 | + message: "outer", |
| 86 | + name: "Error", |
| 87 | + cause: { |
| 88 | + message: "inner", |
| 89 | + name: "RangeError", |
| 90 | + }, |
| 91 | + }); |
| 92 | + expect(error).toBeInstanceOf(Error); |
| 93 | + expect(error.message).toBe("outer"); |
| 94 | + expect(error.cause).toBeInstanceOf(RangeError); |
| 95 | + expect((error.cause as Error).message).toBe("inner"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should fall back to Error for unknown error names", async () => { |
| 99 | + const { reviveError } = await import("../index"); |
| 100 | + const error = reviveError({ |
| 101 | + message: "custom", |
| 102 | + name: "CustomError", |
| 103 | + }); |
| 104 | + expect(error).toBeInstanceOf(Error); |
| 105 | + expect(error.name).toBe("CustomError"); |
| 106 | + }); |
| 107 | +}); |
0 commit comments