@@ -3,6 +3,7 @@ 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 { settings } from '@oclif/core'
67import { beforeEach , describe , expect , test , vi } from 'vitest'
78
89const onNotify = vi . fn ( )
@@ -22,13 +23,22 @@ vi.mock('@bugsnag/js', () => {
2223vi . mock ( './cli.js' )
2324vi . mock ( './context/local.js' )
2425vi . mock ( '../../public/node/crypto.js' )
26+ vi . mock ( '@oclif/core' , ( ) => ( {
27+ settings : {
28+ debug : false ,
29+ } ,
30+ Interfaces : { } ,
31+ } ) )
2532
2633beforeEach ( ( ) => {
2734 vi . mocked ( ciPlatform ) . mockReturnValue ( { isCI : true , name : 'vitest' , metadata : { } } )
2835 vi . mocked ( macAddress ) . mockResolvedValue ( 'macAddress' )
2936 vi . mocked ( cloudEnvironment ) . mockReturnValue ( { platform : 'localhost' , editor : false } )
3037 vi . mocked ( hashString ) . mockReturnValue ( 'hashed-macaddress' )
3138 vi . mocked ( isUnitTest ) . mockReturnValue ( true )
39+ onNotify . mockClear ( )
40+ delete process . env . SHOPIFY_SERVICE_ENV
41+ vi . mocked ( settings ) . debug = false
3242} )
3343
3444describe ( 'errorHandler' , async ( ) => {
@@ -108,7 +118,43 @@ describe('bugsnag metadata', () => {
108118 } )
109119} )
110120
111- describe ( 'send to Bugsnag' , ( ) => {
121+ describe ( 'skips sending errors to Bugsnag' , ( ) => {
122+ test ( 'when SHOPIFY_SERVICE_ENV is local' , async ( ) => {
123+ // Given
124+ process . env . SHOPIFY_SERVICE_ENV = 'local'
125+ const mockOutput = mockAndCaptureOutput ( )
126+ const toThrow = new Error ( 'In test' )
127+
128+ // When
129+ const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
130+
131+ // Then
132+ expect ( res . reported ) . toEqual ( false )
133+ expect ( res . error ) . toEqual ( toThrow )
134+ expect ( res . unhandled ) . toBeUndefined ( )
135+ expect ( onNotify ) . not . toHaveBeenCalled ( )
136+ expect ( mockOutput . debug ( ) ) . toMatch ( 'Skipping Bugsnag report' )
137+ } )
138+
139+ test ( 'when settings.debug is true' , async ( ) => {
140+ // Given
141+ vi . mocked ( settings ) . debug = true
142+ const mockOutput = mockAndCaptureOutput ( )
143+ const toThrow = new Error ( 'In test' )
144+
145+ // When
146+ const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
147+
148+ // Then
149+ expect ( res . reported ) . toEqual ( false )
150+ expect ( res . error ) . toEqual ( toThrow )
151+ expect ( res . unhandled ) . toBeUndefined ( )
152+ expect ( onNotify ) . not . toHaveBeenCalled ( )
153+ expect ( mockOutput . debug ( ) ) . toMatch ( 'Skipping Bugsnag report' )
154+ } )
155+ } )
156+
157+ describe ( 'sends errors to Bugsnag' , ( ) => {
112158 test ( 'processes Error instances as unhandled' , async ( ) => {
113159 const toThrow = new Error ( 'In test' )
114160 const res = await sendErrorToBugsnag ( toThrow , 'unexpected_error' )
0 commit comments