@@ -99,10 +99,32 @@ describe('BugSplat', function () {
9999 bugsplat = new BugSplat ( database , appName , appVersion ) ;
100100 // @ts -expect-error -- accessing private field for test mocking
101101 bugsplat . _formData = ( ) => fakeFormData ;
102- fetchSpy = vi . spyOn ( globalThis , 'fetch' ) as unknown as Mock ;
102+ fetchSpy = vi . fn ( ) ;
103+ // @ts -expect-error -- accessing private field for test mocking
104+ bugsplat . _fetch = fetchSpy ;
103105 } ) ;
104106
105107 describe ( 'post' , ( ) => {
108+ it ( 'should use injectable _fetch instead of globalThis.fetch' , async ( ) => {
109+ const customFetch = vi . fn ( ) . mockResolvedValue ( fakeSuccessResponseBody ) ;
110+ const globalFetchSpy = vi . fn ( ) . mockResolvedValue ( fakeSuccessResponseBody ) ;
111+ vi . stubGlobal ( 'fetch' , globalFetchSpy ) ;
112+ // @ts -expect-error -- accessing private field for test
113+ bugsplat . _fetch = customFetch ;
114+
115+ await bugsplat . post ( new Error ( 'BugSplat!' ) ) ;
116+
117+ expect ( customFetch ) . toHaveBeenCalledOnce ( ) ;
118+ expect ( customFetch ) . toHaveBeenCalledWith (
119+ `https://${ database } .bugsplat.com/post/js/` ,
120+ expect . objectContaining ( {
121+ method : 'POST' ,
122+ body : fakeFormData ,
123+ } )
124+ ) ;
125+ expect ( globalFetchSpy ) . not . toHaveBeenCalled ( ) ;
126+ } ) ;
127+
106128 it ( 'should call fetch url containing database' , async ( ) => {
107129 fetchSpy . mockResolvedValue ( fakeSuccessResponseBody ) ;
108130
0 commit comments