Add hermetic coverage for service boundaries

This commit is contained in:
ms2k 2026-08-24 16:39:13 +09:00
commit de4f990492

View file

@ -0,0 +1,39 @@
import { afterEach, expect, test, vi } from 'vitest'
import { installClientErrorReporting } from './observability'
afterEach(() => vi.restoreAllMocks())
test('reports and cleans up', () => {
const beacon = vi.spyOn(navigator, 'sendBeacon').mockReturnValue(true)
const added = vi.spyOn(window, 'addEventListener')
const removed = vi.spyOn(window, 'removeEventListener')
const remove = installClientErrorReporting()
const error = added.mock.calls.find(([kind]) => kind === 'error')?.[1]
const rejection = added.mock.calls.find(
([kind]) => kind === 'unhandledrejection',
)?.[1]
expect(error).toEqual(expect.any(Function))
expect(rejection).toEqual(expect.any(Function))
;(error as EventListener)(new Event('error'))
;(rejection as EventListener)(new Event('unhandledrejection'))
expect(beacon.mock.calls).toEqual([
[
'/client-errors',
JSON.stringify({ kind: 'error', path: window.location.pathname }),
],
[
'/client-errors',
JSON.stringify({
kind: 'unhandled_rejection',
path: window.location.pathname,
}),
],
])
remove()
expect(removed).toHaveBeenCalledWith('error', error)
expect(removed).toHaveBeenCalledWith('unhandledrejection', rejection)
})