@@ -2,13 +2,20 @@ import * as Sentry from '@sentry/cloudflare';
22import { afterAll , beforeEach , describe , expect , test , vi } from 'vitest' ;
33import { captureRenderErrors } from './captureRenderErrors' ;
44
5- vi . mock ( '@sentry/cloudflare' , ( ) => ( { captureException : vi . fn ( ) } ) ) ;
5+ vi . mock ( '@sentry/cloudflare' , ( ) => ( { captureException : vi . fn ( ) , getIsolationScope : vi . fn ( ) } ) ) ;
6+
7+ function setRequestInScope ( url ?: string ) {
8+ vi . mocked ( Sentry . getIsolationScope ) . mockReturnValue ( {
9+ getScopeData : ( ) => ( { sdkProcessingMetadata : url ? { normalizedRequest : { url } } : { } } ) ,
10+ } as unknown as ReturnType < typeof Sentry . getIsolationScope > ) ;
11+ }
612
713const originalConsoleError = console . error ;
814
915describe ( 'captureRenderErrors' , ( ) => {
1016 beforeEach ( ( ) => {
1117 vi . mocked ( Sentry . captureException ) . mockClear ( ) ;
18+ setRequestInScope ( 'https://wesbos.com/blog' ) ;
1219 console . error = vi . fn ( ) ;
1320 captureRenderErrors ( ) ;
1421 } ) ;
@@ -32,6 +39,17 @@ describe('captureRenderErrors', () => {
3239 expect ( Sentry . captureException ) . not . toHaveBeenCalled ( ) ;
3340 } ) ;
3441
42+ test ( 'ignores a lost connection from outside a request, because the visitor left' , ( ) => {
43+ setRequestInScope ( ) ;
44+ console . error ( 'Error during rendering:' , 'Error: Network connection lost.' ) ;
45+ expect ( Sentry . captureException ) . not . toHaveBeenCalled ( ) ;
46+ } ) ;
47+
48+ test ( 'reports a lost connection from inside a request, because a subrequest failed' , ( ) => {
49+ console . error ( 'Error during rendering:' , 'Error: Network connection lost.' ) ;
50+ expect ( Sentry . captureException ) . toHaveBeenCalledOnce ( ) ;
51+ } ) ;
52+
3553 test ( 'ignores other console errors' , ( ) => {
3654 console . error ( 'Cache read error:' , 'nope' ) ;
3755 expect ( Sentry . captureException ) . not . toHaveBeenCalled ( ) ;
0 commit comments