From af42113fca38effffa17674ea296c71b78c44a5d Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 17 May 2026 10:25:30 +0200 Subject: [PATCH] test(observability): add hooks.client.test.ts unit tests for handleError callback Two tests matching the existing hooks.server.test.ts coverage: returns Sentry lastEventId as errorId; falls back to crypto.randomUUID when lastEventId returns undefined. Co-Authored-By: Claude Sonnet 4.6 --- frontend/src/hooks.client.test.ts | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 frontend/src/hooks.client.test.ts diff --git a/frontend/src/hooks.client.test.ts b/frontend/src/hooks.client.test.ts new file mode 100644 index 00000000..46cb8ad2 --- /dev/null +++ b/frontend/src/hooks.client.test.ts @@ -0,0 +1,47 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +vi.mock('@sentry/sveltekit', () => ({ + init: vi.fn(), + handleErrorWithSentry: (fn: (args: unknown) => unknown) => fn, + lastEventId: vi.fn(() => 'sentry-event-id-abc123') +})); + +describe('hooks.client handleError', () => { + beforeEach(() => { + vi.resetModules(); + }); + + it('returns Sentry lastEventId as errorId', async () => { + const Sentry = await import('@sentry/sveltekit'); + vi.mocked(Sentry.lastEventId).mockReturnValue('sentry-event-id-abc123'); + + const { handleError } = await import('./hooks.client'); + const result = (handleError as (args: unknown) => { message: string; errorId: string })({ + error: new Error('boom'), + event: {}, + status: 500, + message: 'Internal Error' + }); + + expect(result.errorId).toBe('sentry-event-id-abc123'); + expect(result.message).toBe('An unexpected error occurred'); + }); + + it('falls back to crypto.randomUUID when lastEventId returns undefined', async () => { + const Sentry = await import('@sentry/sveltekit'); + vi.mocked(Sentry.lastEventId).mockReturnValue(undefined); + + const { handleError } = await import('./hooks.client'); + const result = (handleError as (args: unknown) => { message: string; errorId: string })({ + error: new Error('boom'), + event: {}, + status: 500, + message: 'Internal Error' + }); + + expect(result.errorId).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ + ); + expect(result.message).toBe('An unexpected error occurred'); + }); +});