|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const captureException = vi.fn() |
| 4 | +const isEnabled = vi.fn() |
| 5 | +const addError = vi.fn() |
| 6 | +const getInitConfiguration = vi.fn() |
| 7 | + |
| 8 | +vi.mock('@sentry/vue', () => ({ |
| 9 | + captureException: (...args: unknown[]) => captureException(...args), |
| 10 | + isEnabled: () => isEnabled() |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@datadog/browser-rum', () => ({ |
| 14 | + datadogRum: { |
| 15 | + addError: (...args: unknown[]) => addError(...args), |
| 16 | + getInitConfiguration: () => getInitConfiguration() |
| 17 | + } |
| 18 | +})) |
| 19 | + |
| 20 | +async function loadReportError() { |
| 21 | + vi.resetModules() |
| 22 | + return import('./reportError') |
| 23 | +} |
| 24 | + |
| 25 | +const sentryLive = (live: boolean) => isEnabled.mockReturnValue(live) |
| 26 | +const datadogLive = (live: boolean) => |
| 27 | + getInitConfiguration.mockReturnValue(live ? {} : undefined) |
| 28 | + |
| 29 | +describe('reportError', () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks() |
| 32 | + sentryLive(true) |
| 33 | + datadogLive(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it('reaches both Sentry and Datadog from a single call', async () => { |
| 37 | + const { reportError } = await loadReportError() |
| 38 | + const error = new Error('boom') |
| 39 | + |
| 40 | + reportError(error, { |
| 41 | + errorType: 'workspace_auth_gate_initialization_failure' |
| 42 | + }) |
| 43 | + |
| 44 | + expect(captureException).toHaveBeenCalledWith( |
| 45 | + error, |
| 46 | + expect.objectContaining({ |
| 47 | + tags: expect.objectContaining({ |
| 48 | + error_type: 'workspace_auth_gate_initialization_failure' |
| 49 | + }) |
| 50 | + }) |
| 51 | + ) |
| 52 | + expect(addError).toHaveBeenCalledWith( |
| 53 | + error, |
| 54 | + expect.objectContaining({ |
| 55 | + error_type: 'workspace_auth_gate_initialization_failure' |
| 56 | + }) |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + it('still reports to Datadog when Sentry is inert', async () => { |
| 61 | + sentryLive(false) |
| 62 | + const { reportError } = await loadReportError() |
| 63 | + |
| 64 | + reportError(new Error('boom'), { errorType: 'bootstrap_auth_wait_timeout' }) |
| 65 | + |
| 66 | + expect(captureException).not.toHaveBeenCalled() |
| 67 | + expect(addError).toHaveBeenCalledOnce() |
| 68 | + }) |
| 69 | + |
| 70 | + it('buffers reports raised before any sink is live, then flushes them', async () => { |
| 71 | + sentryLive(false) |
| 72 | + datadogLive(false) |
| 73 | + const { reportError, flushErrorReports } = await loadReportError() |
| 74 | + |
| 75 | + reportError(new Error('early'), { errorType: 'resource_load_error' }) |
| 76 | + expect(addError).not.toHaveBeenCalled() |
| 77 | + |
| 78 | + datadogLive(true) |
| 79 | + flushErrorReports() |
| 80 | + |
| 81 | + expect(addError).toHaveBeenCalledWith( |
| 82 | + expect.objectContaining({ message: 'early' }), |
| 83 | + expect.objectContaining({ error_type: 'resource_load_error' }) |
| 84 | + ) |
| 85 | + }) |
| 86 | + |
| 87 | + it('does not replay a buffered report twice', async () => { |
| 88 | + sentryLive(false) |
| 89 | + datadogLive(false) |
| 90 | + const { reportError, flushErrorReports } = await loadReportError() |
| 91 | + |
| 92 | + reportError(new Error('early'), { errorType: 'resource_load_error' }) |
| 93 | + |
| 94 | + datadogLive(true) |
| 95 | + flushErrorReports() |
| 96 | + flushErrorReports() |
| 97 | + |
| 98 | + expect(addError).toHaveBeenCalledOnce() |
| 99 | + }) |
| 100 | + |
| 101 | + it('bounds the buffer so a boot-time error storm cannot grow without limit', async () => { |
| 102 | + sentryLive(false) |
| 103 | + datadogLive(false) |
| 104 | + const { reportError, flushErrorReports } = await loadReportError() |
| 105 | + |
| 106 | + for (let i = 0; i < 200; i++) { |
| 107 | + reportError(new Error(`e${i}`), { errorType: 'resource_load_error' }) |
| 108 | + } |
| 109 | + |
| 110 | + datadogLive(true) |
| 111 | + flushErrorReports() |
| 112 | + |
| 113 | + expect(addError.mock.calls.length).toBeLessThanOrEqual(25) |
| 114 | + }) |
| 115 | + |
| 116 | + it('normalizes a non-Error cause', async () => { |
| 117 | + const { reportError } = await loadReportError() |
| 118 | + |
| 119 | + reportError('just a string', { errorType: 'bootstrap_auth_wait_timeout' }) |
| 120 | + |
| 121 | + expect(addError).toHaveBeenCalledWith( |
| 122 | + expect.objectContaining({ message: 'just a string' }), |
| 123 | + expect.anything() |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + it('drops undefined tag values rather than forwarding them', async () => { |
| 128 | + const { reportError } = await loadReportError() |
| 129 | + |
| 130 | + reportError(new Error('boom'), { |
| 131 | + errorType: 'http_error', |
| 132 | + tags: { api_endpoint: '/settings/{key}', http_status: undefined } |
| 133 | + }) |
| 134 | + |
| 135 | + const [, context] = addError.mock.calls[0] |
| 136 | + expect(context).not.toHaveProperty('http_status') |
| 137 | + expect(context).toMatchObject({ api_endpoint: '/settings/{key}' }) |
| 138 | + }) |
| 139 | + |
| 140 | + it('does not throw out of flushErrorReports when a sink throws', async () => { |
| 141 | + sentryLive(false) |
| 142 | + datadogLive(false) |
| 143 | + const { reportError, flushErrorReports } = await loadReportError() |
| 144 | + |
| 145 | + reportError(new Error('early'), { errorType: 'resource_load_error' }) |
| 146 | + |
| 147 | + datadogLive(true) |
| 148 | + addError.mockImplementation(() => { |
| 149 | + throw new Error('datadog exploded') |
| 150 | + }) |
| 151 | + |
| 152 | + expect(() => flushErrorReports()).not.toThrow() |
| 153 | + }) |
| 154 | + |
| 155 | + it('does not throw when a sink throws', async () => { |
| 156 | + captureException.mockImplementation(() => { |
| 157 | + throw new Error('sentry exploded') |
| 158 | + }) |
| 159 | + const { reportError } = await loadReportError() |
| 160 | + |
| 161 | + expect(() => |
| 162 | + reportError(new Error('boom'), { |
| 163 | + errorType: 'bootstrap_auth_wait_timeout' |
| 164 | + }) |
| 165 | + ).not.toThrow() |
| 166 | + }) |
| 167 | +}) |
0 commit comments