@@ -3,6 +3,8 @@ import {ciPlatform, cloudEnvironment, isUnitTest, macAddress} from './context/lo
33import { mockAndCaptureOutput } from './testing/output.js'
44import * as error from './error.js'
55import { hashString } from '../../public/node/crypto.js'
6+ import { isLocalEnvironment } from '../../private/node/context/service.js'
7+ import { settings } from '@oclif/core'
68import { beforeEach , describe , expect , test , vi } from 'vitest'
79
810const onNotify = vi . fn ( )
@@ -22,13 +24,23 @@ vi.mock('@bugsnag/js', () => {
2224vi . mock ( './cli.js' )
2325vi . mock ( './context/local.js' )
2426vi . mock ( '../../public/node/crypto.js' )
27+ vi . mock ( '../../private/node/context/service.js' )
28+ vi . mock ( '@oclif/core' , ( ) => ( {
29+ settings : {
30+ debug : false ,
31+ } ,
32+ Interfaces : { } ,
33+ } ) )
2534
2635beforeEach ( ( ) => {
2736 vi . mocked ( ciPlatform ) . mockReturnValue ( { isCI : true , name : 'vitest' , metadata : { } } )
2837 vi . mocked ( macAddress ) . mockResolvedValue ( 'macAddress' )
2938 vi . mocked ( cloudEnvironment ) . mockReturnValue ( { platform : 'localhost' , editor : false } )
3039 vi . mocked ( hashString ) . mockReturnValue ( 'hashed-macaddress' )
3140 vi . mocked ( isUnitTest ) . mockReturnValue ( true )
41+ onNotify . mockClear ( )
42+ vi . mocked ( settings ) . debug = false
43+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( false )
3244} )
3345
3446describe ( 'errorHandler' , async ( ) => {
@@ -108,7 +120,43 @@ describe('bugsnag metadata', () => {
108120 } )
109121} )
110122
111- describe ( 'send to Bugsnag' , ( ) => {
123+ describe ( 'skips sending errors to Bugsnag' , ( ) => {
124+ test ( 'when using local services' , async ( ) => {
125+ // Given
126+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
127+ const mockOutput = mockAndCaptureOutput ( )
128+ const toThrow = new Error ( 'In test' )
129+
130+ // When
131+ const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
132+
133+ // Then
134+ expect ( res . reported ) . toEqual ( false )
135+ expect ( res . error ) . toEqual ( toThrow )
136+ expect ( res . unhandled ) . toBeUndefined ( )
137+ expect ( onNotify ) . not . toHaveBeenCalled ( )
138+ expect ( mockOutput . debug ( ) ) . toMatch ( 'Skipping Bugsnag report' )
139+ } )
140+
141+ test ( 'when settings.debug is true' , async ( ) => {
142+ // Given
143+ vi . mocked ( settings ) . debug = true
144+ const mockOutput = mockAndCaptureOutput ( )
145+ const toThrow = new Error ( 'In test' )
146+
147+ // When
148+ const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
149+
150+ // Then
151+ expect ( res . reported ) . toEqual ( false )
152+ expect ( res . error ) . toEqual ( toThrow )
153+ expect ( res . unhandled ) . toBeUndefined ( )
154+ expect ( onNotify ) . not . toHaveBeenCalled ( )
155+ expect ( mockOutput . debug ( ) ) . toMatch ( 'Skipping Bugsnag report' )
156+ } )
157+ } )
158+
159+ describe ( 'sends errors to Bugsnag' , ( ) => {
112160 test ( 'processes Error instances as unhandled' , async ( ) => {
113161 const toThrow = new Error ( 'In test' )
114162 const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
0 commit comments