11import assert from 'node:assert/strict'
22import test from 'node:test'
3- import { BrowserClient , Scope , defaultStackParser } from '@sentry/browser'
3+ import {
4+ BrowserClient ,
5+ Scope ,
6+ defaultStackParser ,
7+ getCurrentScope ,
8+ } from '@sentry/browser'
49import {
510 consumeStaleAppRecovery ,
611 reloadOnStaleAppError ,
@@ -33,14 +38,18 @@ test('Sentry suppresses recovered errors but reports cooldown failures', async (
3338 } )
3439 t . mock . method ( Date , 'now' , ( ) => now )
3540 let sent = 0
41+ let sentMetrics = 0
42+ const metrics : unknown [ ] = [ ]
3643 const originals : unknown [ ] = [ ]
3744 const client = new BrowserClient ( {
3845 dsn : 'https://fixture@example.invalid/1' ,
3946 integrations : [ ] ,
4047 stackParser : defaultStackParser ,
4148 transport : ( ) => ( {
42- send : async ( ) => {
43- sent ++
49+ send : async ( [ , items ] ) => {
50+ if ( items . some ( ( [ header ] ) => header . type === 'trace_metric' ) )
51+ sentMetrics ++
52+ else sent ++
4453 return { statusCode : 200 }
4554 } ,
4655 flush : async ( ) => true ,
@@ -49,10 +58,22 @@ test('Sentry suppresses recovered errors but reports cooldown failures', async (
4958 originals . push ( hint . originalException )
5059 return consumeStaleAppRecovery ( hint . originalException ) ? null : event
5160 } ,
61+ beforeSendMetric ( metric ) {
62+ metrics . push ( {
63+ name : metric . name ,
64+ type : metric . type ,
65+ value : metric . value ,
66+ } )
67+ return metric
68+ } ,
5269 } )
5370 t . after ( ( ) => client . close ( 100 ) )
5471 const scope = new Scope ( )
5572 scope . setClient ( client )
73+ const currentScope = getCurrentScope ( )
74+ const previousClient = currentScope . getClient ( )
75+ currentScope . setClient ( client )
76+ t . after ( ( ) => currentScope . setClient ( previousClient ) )
5677 const first = new Error ( 'Failed to fetch dynamically imported module' )
5778 assert . equal ( reloadOnStaleAppError ( [ first ] ) , true )
5879 // The boundary can see the same error again before Sentry processes it.
@@ -91,4 +112,7 @@ test('Sentry suppresses recovered errors but reports cooldown failures', async (
91112 const blocked = new Error ( first . message )
92113 assert . throws ( ( ) => reloadOnStaleAppError ( blocked ) , / r e l o a d b l o c k e d / )
93114 assert . equal ( consumeStaleAppRecovery ( blocked ) , false )
115+ const reloadMetric = { name : 'app.stale_reload' , type : 'counter' , value : 1 }
116+ assert . deepEqual ( metrics , [ reloadMetric , reloadMetric ] )
117+ assert . equal ( sentMetrics , 2 )
94118} )
0 commit comments