@@ -3,6 +3,7 @@ import { Hub } from '@sentry/hub';
3
3
import * as sentryHub from '@sentry/hub' ;
4
4
import { Transaction } from '@sentry/tracing' ;
5
5
import { Runtime } from '@sentry/types' ;
6
+ import { SentryError } from '@sentry/utils' ;
6
7
import * as http from 'http' ;
7
8
import * as net from 'net' ;
8
9
@@ -16,6 +17,7 @@ import {
16
17
requestHandler ,
17
18
tracingHandler ,
18
19
} from '../src/handlers' ;
20
+ import * as SDK from '../src/sdk' ;
19
21
20
22
describe ( 'parseRequest' , ( ) => {
21
23
let mockReq : { [ key : string ] : any } ;
@@ -281,6 +283,31 @@ describe('requestHandler', () => {
281
283
done ( ) ;
282
284
} ) ;
283
285
} ) ;
286
+
287
+ it ( 'patches `res.end` when `flushTimeout` is specified' , ( ) => {
288
+ const flush = jest . spyOn ( SDK , 'flush' ) . mockResolvedValue ( true ) ;
289
+
290
+ const sentryRequestMiddleware = requestHandler ( { flushTimeout : 1337 } ) ;
291
+ sentryRequestMiddleware ( req , res , next ) ;
292
+ res . end ( 'ok' ) ;
293
+
294
+ setImmediate ( ( ) => {
295
+ expect ( flush ) . toHaveBeenCalledWith ( 1337 ) ;
296
+ expect ( res . finished ) . toBe ( true ) ;
297
+ } ) ;
298
+ } ) ;
299
+
300
+ it ( 'prevents errors thrown during `flush` from breaking the response' , async ( ) => {
301
+ jest . spyOn ( SDK , 'flush' ) . mockRejectedValue ( new SentryError ( 'HTTP Error (429)' ) ) ;
302
+
303
+ const sentryRequestMiddleware = requestHandler ( { flushTimeout : 1337 } ) ;
304
+ sentryRequestMiddleware ( req , res , next ) ;
305
+ res . end ( 'ok' ) ;
306
+
307
+ setImmediate ( ( ) => {
308
+ expect ( res . finished ) . toBe ( true ) ;
309
+ } ) ;
310
+ } ) ;
284
311
} ) ;
285
312
286
313
describe ( 'tracingHandler' , ( ) => {
0 commit comments