From 81d6947b531b787027beb6dd84f9ba35f4c11e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Antonio=20Fern=C3=A1ndez=20de=20Alba?= Date: Wed, 23 Oct 2024 11:39:57 +0200 Subject: [PATCH] [test visibility] Add errors in retried tests in mocha (#4813) --- integration-tests/mocha/mocha.spec.js | 6 +++++- packages/datadog-instrumentations/src/mocha/utils.js | 4 ++-- packages/datadog-plugin-mocha/src/index.js | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/integration-tests/mocha/mocha.spec.js b/integration-tests/mocha/mocha.spec.js index dac0a9e3bff..3fa11871204 100644 --- a/integration-tests/mocha/mocha.spec.js +++ b/integration-tests/mocha/mocha.spec.js @@ -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, @@ -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) diff --git a/packages/datadog-instrumentations/src/mocha/utils.js b/packages/datadog-instrumentations/src/mocha/utils.js index a4da0762039..2b51fd6e73b 100644 --- a/packages/datadog-instrumentations/src/mocha/utils.js +++ b/packages/datadog-instrumentations/src/mocha/utils.js @@ -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) diff --git a/packages/datadog-plugin-mocha/src/index.js b/packages/datadog-plugin-mocha/src/index.js index 30f6e88a9fc..0513a4a95d6 100644 --- a/packages/datadog-plugin-mocha/src/index.js +++ b/packages/datadog-plugin-mocha/src/index.js @@ -242,7 +242,7 @@ 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) { @@ -250,6 +250,9 @@ class MochaPlugin extends CiPlugin { if (!isFirstAttempt) { span.setTag(TEST_IS_RETRY, 'true') } + if (err) { + span.setTag('error', err) + } const spanTags = span.context()._tags this.telemetry.ciVisEvent(