Add hermetic coverage for service boundaries
This commit is contained in:
parent
19ac156f4b
commit
de4f990492
1 changed files with 137 additions and 0 deletions
39
web/src/obs.browser.test.ts
Normal file
39
web/src/obs.browser.test.ts
Normal 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)
|
||||
})
|
||||
Loading…
Reference in a new issue