Skip to content

Commit

Permalink
[ci-visibility] Mocha: fix this.retries (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Oct 13, 2021
1 parent 6c55168 commit acbf769
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/datadog-plugin-mocha/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function getTestSpanMetadata (tracer, test, sourceRoot) {
function createWrapRunTest (tracer, testEnvironmentMetadata, sourceRoot) {
return function wrapRunTest (runTest) {
return async function runTestWithTrace () {
// `runTest` is rerun when retries are configured through `this.retries` and the test fails.
// This clause prevents rewrapping `this.test.fn` when it has already been wrapped.
if (this.test._currentRetry !== undefined && this.test._currentRetry !== 0) {
return runTest.apply(this, arguments)
}

let specFunction = this.test.fn
if (specFunction.length) {
specFunction = promisify(specFunction)
Expand Down
38 changes: 38 additions & 0 deletions packages/datadog-plugin-mocha/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,44 @@ Timeout of 100ms exceeded. For async tests and hooks, ensure "done()" is called;
mocha.addFile(testFilePath)
mocha.run()
})

it('works with retries', (done) => {
const testFilePath = path.join(__dirname, 'mocha-test-retries.js')

let numTestSpans = 0

// Handler that always fails to be run for every trace that is generated.
// This way, the number of test spans is counted.
agent.use(trace => {
const testSpan = trace[0][0]
if (testSpan.type === 'test') {
numTestSpans++
}
expect(true).to.equal(false)
})

const assertionPromises = ['fail', 'pass'].map((testStatus, index) => {
return agent.use(trace => {
const testSpan = trace[0][0]
// expect(testSpan.meta.attempt).to.equal(`${index}`)
expect(testSpan.meta[TEST_STATUS]).to.equal(testStatus)
})
})

Promise.all(assertionPromises)
.then(() => {
// it will fail twice and pass at the third time
expect(numTestSpans).to.equal(3)
done()
})
.catch(done)

const mocha = new Mocha({
reporter: function () {} // silent on internal tests
})
mocha.addFile(testFilePath)
mocha.run()
})
})
})
})
9 changes: 9 additions & 0 deletions packages/datadog-plugin-mocha/test/mocha-test-retries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { expect } = require('chai')

let attempt = 0
describe('mocha-test-retries', function () {
this.retries(4)
it('will be retried', () => {
expect(attempt++).to.equal(2)
})
})

0 comments on commit acbf769

Please sign in to comment.