@@ -18,19 +18,26 @@ const MOCK_ENDPOINT = "http://www.example.com";
1818
1919// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2020// @ts -ignore
21- global . navigator . sendBeacon = ( url : string , content : string ) : boolean => {
22- void fetch ( url , {
23- body : content ,
24- method : "POST" ,
25- } ) ;
21+ // eslint-disable-next-line jsdoc/require-jsdoc
22+ function setGlobalSendBeacon ( ) {
23+ global . navigator . sendBeacon = ( url : string , content : string ) : boolean => {
24+ void fetch ( url , {
25+ body : content ,
26+ method : "POST" ,
27+ } ) ;
2628
27- return true ;
28- } ;
29+ return true ;
30+ } ;
31+ }
2932// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3033// @ts -ignore
3134global . fetch = fetch ;
3235
3336describe ( "Uploader/BrowserSendBeaconFallback" , function ( ) {
37+ beforeEach ( function ( ) {
38+ setGlobalSendBeacon ( ) ;
39+ } ) ;
40+
3441 afterEach ( function ( ) {
3542 sandbox . restore ( ) ;
3643 } ) ;
@@ -57,7 +64,14 @@ describe("Uploader/BrowserSendBeaconFallback", function () {
5764 const TEST_PING_CONTENT = { "my-test-value" : 40721 } ;
5865 nock ( MOCK_ENDPOINT ) . post ( / ./ i) . reply ( 200 ) ;
5966
67+ // Reset `fetch` to a known state.
68+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69+ // @ts -ignore
70+ global . fetch = fetch ;
71+
72+ // Ensure `sendBeacon` fails.
6073 global . navigator . sendBeacon = ( ) => false ;
74+
6175 const response = BrowserSendBeaconFallbackUploader . post ( MOCK_ENDPOINT , new PingRequest ( "abc" , { } , JSON . stringify ( TEST_PING_CONTENT ) , 1024 ) ) ;
6276 const expectedResponse = new UploadResult ( UploadResultStatus . Success , 200 ) ;
6377 assert . deepStrictEqual (
@@ -72,7 +86,14 @@ describe("Uploader/BrowserSendBeaconFallback", function () {
7286 code : "AWFUL_ERROR" ,
7387 } ) ;
7488
89+ // Reset `fetch` to a known state.
90+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
91+ // @ts -ignore
92+ global . fetch = fetch ;
93+
94+ // Ensure `sendBeacon` fails.
7595 global . navigator . sendBeacon = ( ) => false ;
96+
7697 const response = BrowserSendBeaconFallbackUploader . post ( MOCK_ENDPOINT , new PingRequest ( "abc" , { } , "{}" , 1024 ) ) ;
7798 const expectedResponse = new UploadResult ( UploadResultStatus . RecoverableFailure ) ;
7899 assert . deepStrictEqual (
0 commit comments