Skip to content

Commit

Permalink
Fix flaky test in rewriter.spec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Feb 6, 2025
1 parent f49e6ff commit 39c2924
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/dd-trace/test/appsec/iast/taint-tracking/rewriter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ describe('IAST Rewriter', () => {
'@datadog/native-iast-rewriter': {
Rewriter,
getPrepareStackTrace: function (fn) {
const testWrap = function testWrappedPrepareStackTrace (_, callsites) {
return fn(_, callsites)
const testWrap = function testWrappedPrepareStackTrace (error, callsites) {
if (typeof fn !== 'function') {
return error.stack
}

return fn?.(error, callsites)
}

Object.defineProperty(testWrap, kSymbolPrepareStackTrace, {
value: true
})
Expand Down Expand Up @@ -219,6 +224,21 @@ describe('IAST Rewriter', () => {
describe('thread communication', () => {
let port

function waitUntilCheckSuccess (check, maxMs = 500) {
setTimeout(() => {
try {
check()
} catch (e) {
if (maxMs > 0) {
waitUntilCheckSuccess(check, maxMs - 10)
return
}

throw e
}
}, 10)
}

beforeEach(() => {
process.execArgv = ['--loader', 'dd-trace/initialize.mjs']
rewriter.enableRewriter()
Expand All @@ -237,7 +257,7 @@ describe('IAST Rewriter', () => {

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

setTimeout(() => {
waitUntilCheckSuccess(() => {
expect(cacheRewrittenSourceMap).to.be.calledOnceWith('file.js', content)

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

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

setTimeout(() => {
waitUntilCheckSuccess(() => {
expect(rewriterTelemetry.incrementTelemetryIfNeeded).to.be.calledOnceWith(metrics)

done()
Expand Down Expand Up @@ -290,7 +310,7 @@ describe('IAST Rewriter', () => {

setTimeout(() => {
hardcodedSecretCh.unsubscribe(onHardcodedSecret)
})
}, 50)
})

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

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

setTimeout(() => {
waitUntilCheckSuccess(() => {
expect(log.error).to.be.calledOnceWith(...messages)

done()
})
})
Expand Down

0 comments on commit 39c2924

Please sign in to comment.