|
| 1 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + startup: vi.fn(), |
| 5 | + clientSubmitException: vi.fn(), |
| 6 | + serverSubmitException: vi.fn() |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("@exceptionless/browser", () => ({ |
| 10 | + Exceptionless: { startup: mocks.startup, submitException: mocks.clientSubmitException }, |
| 11 | + toError: (/** @type {unknown} */ error) => error |
| 12 | +})); |
| 13 | +vi.mock("@exceptionless/node", () => ({ |
| 14 | + Exceptionless: { startup: mocks.startup, submitException: mocks.serverSubmitException }, |
| 15 | + toError: (/** @type {unknown} */ error) => error |
| 16 | +})); |
| 17 | + |
| 18 | +import { handleError as handleClientError } from "../src/hooks.client.js"; |
| 19 | +import { handleError as handleServerError } from "../src/hooks.server.js"; |
| 20 | + |
| 21 | +describe("SvelteKit error hooks", () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + mocks.clientSubmitException.mockResolvedValue(undefined); |
| 25 | + mocks.serverSubmitException.mockResolvedValue(undefined); |
| 26 | + }); |
| 27 | + |
| 28 | + test.each(["app", "framework"])("does not submit expected %s errors from either hook", async (kind) => { |
| 29 | + const input = { kind, error: { status: 404, message: "Not found" }, event: {} }; |
| 30 | + await handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (/** @type {unknown} */ (input))); |
| 31 | + await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input))); |
| 32 | + expect(mocks.clientSubmitException).not.toHaveBeenCalled(); |
| 33 | + expect(mocks.serverSubmitException).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + test("does not submit server validation errors", async () => { |
| 37 | + const input = { kind: "validation", error: { status: 400, message: "Invalid" }, issues: [], event: {} }; |
| 38 | + await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input))); |
| 39 | + expect(mocks.serverSubmitException).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + test("submits unexpected errors from both hooks", async () => { |
| 43 | + const error = new Error("Unexpected failure"); |
| 44 | + const input = { kind: "unknown", error, event: {} }; |
| 45 | + await handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (/** @type {unknown} */ (input))); |
| 46 | + await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input))); |
| 47 | + expect(mocks.clientSubmitException).toHaveBeenCalledWith(error); |
| 48 | + expect(mocks.serverSubmitException).toHaveBeenCalledWith(error); |
| 49 | + }); |
| 50 | +}); |
0 commit comments