Skip to content

Commit 2805a22

Browse files
authored
Fix flaky test in rewriter.spec.js (#5222)
1 parent 218c35a commit 2805a22

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

packages/dd-trace/test/appsec/iast/taint-tracking/rewriter.spec.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ describe('IAST Rewriter', () => {
6969
'@datadog/native-iast-rewriter': {
7070
Rewriter,
7171
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)
7478
}
79+
7580
Object.defineProperty(testWrap, kSymbolPrepareStackTrace, {
7681
value: true
7782
})
@@ -219,6 +224,21 @@ describe('IAST Rewriter', () => {
219224
describe('thread communication', () => {
220225
let port
221226

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+
222242
beforeEach(() => {
223243
process.execArgv = ['--loader', 'dd-trace/initialize.mjs']
224244
rewriter.enableRewriter()
@@ -237,7 +257,7 @@ describe('IAST Rewriter', () => {
237257

238258
port.postMessage({ type: constants.REWRITTEN_MESSAGE, data })
239259

240-
setTimeout(() => {
260+
waitUntilCheckSuccess(() => {
241261
expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file.js', content)
242262

243263
done()
@@ -257,7 +277,7 @@ describe('IAST Rewriter', () => {
257277

258278
port.postMessage({ type: constants.REWRITTEN_MESSAGE, data })
259279

260-
setTimeout(() => {
280+
waitUntilCheckSuccess(() => {
261281
expect(rewriterTelemetry.incrementTelemetryIfNeeded).to.be.calledOnceWith(metrics)
262282

263283
done()
@@ -290,7 +310,7 @@ describe('IAST Rewriter', () => {
290310

291311
setTimeout(() => {
292312
hardcodedSecretCh.unsubscribe(onHardcodedSecret)
293-
})
313+
}, 50)
294314
})
295315

296316
it('should log the message', (done) => {
@@ -302,9 +322,8 @@ describe('IAST Rewriter', () => {
302322

303323
port.postMessage({ type: constants.LOG_MESSAGE, data })
304324

305-
setTimeout(() => {
325+
waitUntilCheckSuccess(() => {
306326
expect(log.error).to.be.calledOnceWith(...messages)
307-
308327
done()
309328
})
310329
})

0 commit comments

Comments
 (0)