@@ -69,9 +69,14 @@ describe('IAST Rewriter', () => {
69
69
'@datadog/native-iast-rewriter' : {
70
70
Rewriter,
71
71
getPrepareStackTrace : function ( fn ) {
72
- const testWrap = function testWrappedPrepareStackTrace ( _ , callsites ) {
73
- return fn ( _ , callsites )
72
+ const testWrap = function testWrappedPrepareStackTrace ( error , callsites ) {
73
+ if ( typeof fn !== 'function' ) {
74
+ return error . stack
75
+ }
76
+
77
+ return fn ?. ( error , callsites )
74
78
}
79
+
75
80
Object . defineProperty ( testWrap , kSymbolPrepareStackTrace , {
76
81
value : true
77
82
} )
@@ -219,6 +224,21 @@ describe('IAST Rewriter', () => {
219
224
describe ( 'thread communication' , ( ) => {
220
225
let port
221
226
227
+ function waitUntilCheckSuccess ( check , maxMs = 500 ) {
228
+ setTimeout ( ( ) => {
229
+ try {
230
+ check ( )
231
+ } catch ( e ) {
232
+ if ( maxMs > 0 ) {
233
+ waitUntilCheckSuccess ( check , maxMs - 10 )
234
+ return
235
+ }
236
+
237
+ throw e
238
+ }
239
+ } , 10 )
240
+ }
241
+
222
242
beforeEach ( ( ) => {
223
243
process . execArgv = [ '--loader' , 'dd-trace/initialize.mjs' ]
224
244
rewriter . enableRewriter ( )
@@ -237,7 +257,7 @@ describe('IAST Rewriter', () => {
237
257
238
258
port . postMessage ( { type : constants . REWRITTEN_MESSAGE , data } )
239
259
240
- setTimeout ( ( ) => {
260
+ waitUntilCheckSuccess ( ( ) => {
241
261
expect ( cacheRewrittenSourceMap ) . to . be . calledOnceWith ( 'file.js' , content )
242
262
243
263
done ( )
@@ -257,7 +277,7 @@ describe('IAST Rewriter', () => {
257
277
258
278
port . postMessage ( { type : constants . REWRITTEN_MESSAGE , data } )
259
279
260
- setTimeout ( ( ) => {
280
+ waitUntilCheckSuccess ( ( ) => {
261
281
expect ( rewriterTelemetry . incrementTelemetryIfNeeded ) . to . be . calledOnceWith ( metrics )
262
282
263
283
done ( )
@@ -290,7 +310,7 @@ describe('IAST Rewriter', () => {
290
310
291
311
setTimeout ( ( ) => {
292
312
hardcodedSecretCh . unsubscribe ( onHardcodedSecret )
293
- } )
313
+ } , 50 )
294
314
} )
295
315
296
316
it ( 'should log the message' , ( done ) => {
@@ -302,9 +322,8 @@ describe('IAST Rewriter', () => {
302
322
303
323
port . postMessage ( { type : constants . LOG_MESSAGE , data } )
304
324
305
- setTimeout ( ( ) => {
325
+ waitUntilCheckSuccess ( ( ) => {
306
326
expect ( log . error ) . to . be . calledOnceWith ( ...messages )
307
-
308
327
done ( )
309
328
} )
310
329
} )
0 commit comments