diff --git a/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js b/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js index e7dac1607c8..3027baff50a 100644 --- a/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +++ b/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js @@ -12,19 +12,7 @@ const { TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES } = require('../../ci-visibility/telemetry') -function getNumTests (knownTests) { - let totalNumTests = 0 - - for (const testModule of Object.values(knownTests)) { - for (const testSuite of Object.values(testModule)) { - for (const testList of Object.values(testSuite)) { - totalNumTests += testList.length - } - } - } - - return totalNumTests -} +const { getNumFromKnownTests } = require('../../plugins/util/test') function getKnownTests ({ url, @@ -102,7 +90,7 @@ function getKnownTests ({ try { const { data: { attributes: { tests: knownTests } } } = JSON.parse(res) - const numTests = getNumTests(knownTests) + const numTests = getNumFromKnownTests(knownTests) incrementCountMetric(TELEMETRY_KNOWN_TESTS_RESPONSE_TESTS, {}, numTests) distributionMetric(TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES, {}, res.length) diff --git a/packages/dd-trace/src/plugins/util/test.js b/packages/dd-trace/src/plugins/util/test.js index e7e60823987..6c0dde70cfb 100644 --- a/packages/dd-trace/src/plugins/util/test.js +++ b/packages/dd-trace/src/plugins/util/test.js @@ -180,7 +180,8 @@ module.exports = { TEST_BROWSER_NAME, TEST_BROWSER_VERSION, getTestSessionName, - TEST_LEVEL_EVENT_TYPES + TEST_LEVEL_EVENT_TYPES, + getNumFromKnownTests } // Returns pkg manager and its version, separated by '-', e.g. npm-8.15.0 or yarn-1.22.19 @@ -618,3 +619,21 @@ function getTestSessionName (config, testCommand, envTags) { } return testCommand } + +// Calculate the number of a tests from the known tests response, which has a shape like: +// { testModule1: { testSuite1: [test1, test2, test3] }, testModule2: { testSuite2: [test4, test5] } } +function getNumFromKnownTests (knownTests) { + if (!knownTests) { + return 0 + } + + let totalNumTests = 0 + + for (const testModule of Object.values(knownTests)) { + for (const testSuite of Object.values(testModule)) { + totalNumTests += testSuite.length + } + } + + return totalNumTests +} diff --git a/packages/dd-trace/test/plugins/util/test.spec.js b/packages/dd-trace/test/plugins/util/test.spec.js index ee321f3c0e0..f79ab8fd34d 100644 --- a/packages/dd-trace/test/plugins/util/test.spec.js +++ b/packages/dd-trace/test/plugins/util/test.spec.js @@ -15,7 +15,8 @@ const { resetCoverage, removeInvalidMetadata, parseAnnotations, - getIsFaultyEarlyFlakeDetection + getIsFaultyEarlyFlakeDetection, + getNumFromKnownTests } = require('../../../src/plugins/util/test') const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA, CI_PIPELINE_URL } = require('../../../src/plugins/util/tags') @@ -335,3 +336,32 @@ describe('getIsFaultyEarlyFlakeDetection', () => { expect(isFaulty).to.be.true }) }) + +describe('getNumFromKnownTests', () => { + it('calculates the number of tests from the known tests', () => { + const knownTests = { + testModule: { + 'test1.spec.js': ['test1', 'test2'], + 'test2.spec.js': ['test3'] + } + } + + const numTests = getNumFromKnownTests(knownTests) + expect(numTests).to.equal(3) + }) + + it('does not crash with empty dictionaries', () => { + const knownTests = {} + + const numTests = getNumFromKnownTests(knownTests) + expect(numTests).to.equal(0) + }) + + it('does not crash if known tests is undefined or null', () => { + const numTestsUndefined = getNumFromKnownTests(undefined) + expect(numTestsUndefined).to.equal(0) + + const numTestsNull = getNumFromKnownTests(null) + expect(numTestsNull).to.equal(0) + }) +})