Skip to content

Commit

Permalink
[test visibility] Add errors in retried tests in mocha (#4813)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored Oct 23, 2024
1 parent 31ab8e5 commit 81d6947
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion integration-tests/mocha/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ describe('mocha CommonJS', function () {
})
})

context('flaky test retries', () => {
context('auto test retries', () => {
it('retries failed tests automatically', (done) => {
receiver.setSettings({
itr_enabled: false,
Expand Down Expand Up @@ -1911,6 +1911,10 @@ describe('mocha CommonJS', function () {
const failedAttempts = tests.filter(test => test.meta[TEST_STATUS] === 'fail')
assert.equal(failedAttempts.length, 2)

failedAttempts.forEach((failedTest, index) => {
assert.include(failedTest.meta[ERROR_MESSAGE], `expected ${index + 1} to equal 3`)
})

// The first attempt is not marked as a retry
const retriedFailure = failedAttempts.filter(test => test.meta[TEST_IS_RETRY] === 'true')
assert.equal(retriedFailure.length, 1)
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-instrumentations/src/mocha/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ function getOnFailHandler (isMain) {
}

function getOnTestRetryHandler () {
return function (test) {
return function (test, err) {
const asyncResource = getTestAsyncResource(test)
if (asyncResource) {
const isFirstAttempt = test._currentRetry === 0
asyncResource.runInAsyncScope(() => {
testRetryCh.publish(isFirstAttempt)
testRetryCh.publish({ isFirstAttempt, err })
})
}
const key = getTestToArKey(test)
Expand Down
5 changes: 4 additions & 1 deletion packages/datadog-plugin-mocha/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,17 @@ class MochaPlugin extends CiPlugin {
}
})

this.addSub('ci:mocha:test:retry', (isFirstAttempt) => {
this.addSub('ci:mocha:test:retry', ({ isFirstAttempt, err }) => {
const store = storage.getStore()
const span = store?.span
if (span) {
span.setTag(TEST_STATUS, 'fail')
if (!isFirstAttempt) {
span.setTag(TEST_IS_RETRY, 'true')
}
if (err) {
span.setTag('error', err)
}

const spanTags = span.context()._tags
this.telemetry.ciVisEvent(
Expand Down

0 comments on commit 81d6947

Please sign in to comment.