@@ -91,6 +91,16 @@ describe('withSentry', () => {
9191 } ) ;
9292
9393 test ( 'flush must be called when all waitUntil are done' , async ( ) => {
94+ // Also spy on the exported flush function to see how many times it's called
95+ let exportedFlushCallCount = 0 ;
96+ const originalExportedFlush = SentryCore . flush ;
97+ const exportedFlushSpy = vi . spyOn ( SentryCore , 'flush' ) . mockImplementation ( async ( timeout ?: number ) => {
98+ exportedFlushCallCount ++ ;
99+ console . log ( `[EXPORTED flush() #${ exportedFlushCallCount } ] timeout: ${ timeout } ` ) ;
100+ // Call through to the real implementation
101+ return originalExportedFlush ( timeout ) ;
102+ } ) ;
103+
94104 let flushCallCount = 0 ;
95105 const originalFlush = SentryCore . Client . prototype . flush ;
96106
@@ -99,15 +109,24 @@ describe('withSentry', () => {
99109 timeout ?: number ,
100110 ) {
101111 flushCallCount ++ ;
102- console . log ( `[FLUSH #${ flushCallCount } ] Client: ${ this ?. constructor ?. name || 'unknown' } , timeout: ${ timeout } ` ) ;
112+ console . log ( `\n [FLUSH #${ flushCallCount } ] Client: ${ this ?. constructor ?. name || 'unknown' } , timeout: ${ timeout } ` ) ;
103113 const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 , 10 ) . join ( '\n ' ) ;
104114 console . log ( ` Stack:\n ${ stack } ` ) ;
105115 console . log ( ` this._flushLock:` , this . _flushLock ? 'exists' : 'undefined' ) ;
106116 console . log (
107117 ` Recursion check: flushCallCount=${ flushCallCount } , spy.calls.length=${ flushSpy . mock . calls . length } ` ,
108118 ) ;
109119
110- // Important: Return a resolved promise to avoid triggering parent flush logic
120+ // Check if we're being called from within another flush
121+ const stackString = new Error ( ) . stack || '' ;
122+ const flushInStack = ( stackString . match ( / \. f l u s h \( / g) || [ ] ) . length ;
123+ console . log ( ` Number of .flush( in stack: ${ flushInStack } ` ) ;
124+
125+ // Try calling the original to see if that changes behavior
126+ // Comment this out to see if it matters:
127+ // return originalFlush.call(this, timeout);
128+
129+ // Or just return immediately:
111130 return Promise . resolve ( true ) ;
112131 } ) ;
113132 flushSpy . mockClear ( ) ; // Explicitly clear before test
@@ -116,28 +135,33 @@ describe('withSentry', () => {
116135 onTestFinished ( ( ) => {
117136 vi . useRealTimers ( ) ;
118137 flushSpy . mockRestore ( ) ;
138+ exportedFlushSpy . mockRestore ( ) ;
119139 } ) ;
120140 let pendingCount = 0 ;
121141 const waits : Promise < unknown > [ ] = [ ] ;
142+
143+ // Track the ORIGINAL waitUntil to see if makeFlushLock wraps it
144+ let originalWaitUntilCallCount = 0 ;
122145 const waitUntil = vi . fn ( promise => {
123- const callNum = waitUntil . mock . calls . length ;
146+ originalWaitUntilCallCount ++ ;
147+ const callNum = originalWaitUntilCallCount ;
124148 pendingCount ++ ;
125149 console . log (
126- `[WAITUNTIL #${ callNum } ] Pending: ${ pendingCount } , Promise type: ${ promise ?. constructor ?. name || 'unknown' } ` ,
150+ `[ORIGINAL_WAITUNTIL #${ callNum } ] Pending: ${ pendingCount } , Promise type: ${ promise ?. constructor ?. name || 'unknown' } ` ,
127151 ) ;
128- const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 , 5 ) . join ( '\n ' ) ;
152+ const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 , 6 ) . join ( '\n ' ) ;
129153 console . log ( ` Stack:\n ${ stack } ` ) ;
130154
131155 // Wrap the promise to log when it resolves
132156 const wrappedPromise = promise . then (
133157 val => {
134158 pendingCount -- ;
135- console . log ( `[WAITUNTIL #${ callNum } RESOLVED] Pending now: ${ pendingCount } ` ) ;
159+ console . log ( `[ORIGINAL_WAITUNTIL #${ callNum } RESOLVED] Pending now: ${ pendingCount } ` ) ;
136160 return val ;
137161 } ,
138162 err => {
139163 pendingCount -- ;
140- console . log ( `[WAITUNTIL #${ callNum } REJECTED] Pending now: ${ pendingCount } ` , err ) ;
164+ console . log ( `[ORIGINAL_WAITUNTIL #${ callNum } REJECTED] Pending now: ${ pendingCount } ` , err ) ;
141165 throw err ;
142166 } ,
143167 ) ;
@@ -175,7 +199,8 @@ describe('withSentry', () => {
175199
176200 console . log ( '\n=== TEST END ===' ) ;
177201 console . log ( `Total waitUntil calls: ${ waitUntil . mock . calls . length } ` ) ;
178- console . log ( `Total flush calls: ${ flushSpy . mock . calls . length } ` ) ;
202+ console . log ( `Total EXPORTED flush() calls: ${ exportedFlushCallCount } ` ) ;
203+ console . log ( `Total Client.flush() calls: ${ flushSpy . mock . calls . length } ` ) ;
179204 console . log ( `Flush calls before resolve: ${ callsBeforeResolve } ` ) ;
180205
181206 // Check that flush was called at least once after waitUntils resolved
@@ -188,10 +213,16 @@ describe('withSentry', () => {
188213 console . error ( '\n=== ASSERTION FAILURE ===' ) ;
189214 console . error ( `Expected exactly 1 NEW flush call, but got ${ delta } ` ) ;
190215 console . error ( `Calls before: ${ callsBeforeResolve } , Calls after: ${ callsAfterResolve } ` ) ;
191- console . error ( '\nAll flush call arguments:' ) ;
216+ console . error ( `\nExported flush() was called ${ exportedFlushCallCount } times` ) ;
217+ console . error ( `Client.flush() was called ${ callsAfterResolve } times` ) ;
218+ console . error ( '\nAll Client.flush() call arguments:' ) ;
192219 flushSpy . mock . calls . forEach ( ( call , i ) => {
193220 console . error ( ` Call ${ i + 1 } :` , call ) ;
194221 } ) ;
222+ console . error ( '\nAll exported flush() call arguments:' ) ;
223+ exportedFlushSpy . mock . calls . forEach ( ( call , i ) => {
224+ console . error ( ` Call ${ i + 1 } :` , call ) ;
225+ } ) ;
195226 }
196227
197228 // Ideally it should be exactly 1, but verify the delta to avoid CI flakiness
0 commit comments