@@ -14,6 +14,7 @@ import {joinPath, dirname} from './path.js'
1414import { publishMonorailEvent } from './monorail.js'
1515import { mockAndCaptureOutput } from './testing/output.js'
1616import { addPublicMetadata } from './metadata.js'
17+ import { sendErrorToBugsnag } from './error-handler.js'
1718import * as store from '../../private/node/analytics/storage.js'
1819import { startAnalytics } from '../../private/node/analytics.js'
1920import { hashString } from '../../public/node/crypto.js'
@@ -29,6 +30,7 @@ vi.mock('../../public/node/crypto.js')
2930vi . mock ( '../../version.js' )
3031vi . mock ( './monorail.js' )
3132vi . mock ( './cli.js' )
33+ vi . mock ( './error-handler.js' )
3234
3335describe ( 'event tracking' , ( ) => {
3436 const currentDate = new Date ( Date . UTC ( 2022 , 1 , 1 , 10 , 0 , 0 ) )
@@ -256,6 +258,36 @@ describe('event tracking', () => {
256258 } )
257259 } )
258260
261+ test ( 'reports telemetry failures to Bugsnag' , async ( ) => {
262+ await inProjectWithFile ( 'package.json' , async ( args ) => {
263+ // Given
264+ const commandContent = { command : 'dev' , topic : 'app' }
265+ const telemetryError = new Error ( 'OTLP endpoint unavailable' )
266+ vi . mocked ( os . platformAndArch ) . mockImplementationOnce ( ( ) => {
267+ throw telemetryError
268+ } )
269+ vi . mocked ( sendErrorToBugsnag ) . mockResolvedValue ( {
270+ error : telemetryError ,
271+ reported : true ,
272+ unhandled : false ,
273+ } )
274+ const outputMock = mockAndCaptureOutput ( )
275+ await startAnalytics ( { commandContent, args} )
276+
277+ // When
278+ const config = {
279+ runHook : vi . fn ( ) . mockResolvedValue ( { successes : [ ] , failures : [ ] } ) ,
280+ plugins : [ ] ,
281+ } as any
282+ await reportAnalyticsEvent ( { config, exitMode : 'ok' } )
283+
284+ // Then
285+ expect ( sendErrorToBugsnag ) . toHaveBeenCalledOnce ( )
286+ expect ( sendErrorToBugsnag ) . toHaveBeenCalledWith ( telemetryError , 'expected_error' )
287+ expect ( outputMock . debug ( ) ) . toMatch ( 'Failed to report usage analytics: OTLP endpoint unavailable' )
288+ } )
289+ } )
290+
259291 describe ( 'recordTiming' , ( ) => {
260292 test ( 'delegates to store.recordTiming' , ( ) => {
261293 // Given
0 commit comments