From 9ddd6a31656572c290e4a909f1ab5c62d6211131 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 15 Oct 2024 17:38:27 +0200 Subject: [PATCH 1/3] fix num tests --- .../ci-visibility/early-flake-detection/get-known-tests.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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..5ce64db5149 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 @@ -17,9 +17,8 @@ function getNumTests (knownTests) { for (const testModule of Object.values(knownTests)) { for (const testSuite of Object.values(testModule)) { - for (const testList of Object.values(testSuite)) { - totalNumTests += testList.length - } + // test suites are arrays of tests + totalNumTests += testSuite.length } } From c36403820805a8d136109df69dabe6a69abc822d Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Wed, 16 Oct 2024 11:57:23 +0200 Subject: [PATCH 2/3] add unit tests --- .../early-flake-detection/get-known-tests.js | 18 ++--------- packages/dd-trace/src/plugins/util/test.js | 17 +++++++++- .../dd-trace/test/plugins/util/test.spec.js | 32 ++++++++++++++++++- 3 files changed, 50 insertions(+), 17 deletions(-) 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 5ce64db5149..daf6931ede5 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 @@ -9,22 +9,10 @@ const { TELEMETRY_KNOWN_TESTS_MS, TELEMETRY_KNOWN_TESTS_ERRORS, TELEMETRY_KNOWN_TESTS_RESPONSE_TESTS, - TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES + TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES, + getNumFromKnownTests } = require('../../ci-visibility/telemetry') -function getNumTests (knownTests) { - let totalNumTests = 0 - - for (const testModule of Object.values(knownTests)) { - for (const testSuite of Object.values(testModule)) { - // test suites are arrays of tests - totalNumTests += testSuite.length - } - } - - return totalNumTests -} - function getKnownTests ({ url, isEvpProxy, @@ -101,7 +89,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..2748e9ab55b 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,17 @@ 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) { + 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) + }) +}) From adeb811f1df3318267b681bacba40fcdfd1db397 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Wed, 16 Oct 2024 12:05:14 +0200 Subject: [PATCH 3/3] fix import --- .../ci-visibility/early-flake-detection/get-known-tests.js | 5 +++-- packages/dd-trace/src/plugins/util/test.js | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) 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 daf6931ede5..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 @@ -9,10 +9,11 @@ const { TELEMETRY_KNOWN_TESTS_MS, TELEMETRY_KNOWN_TESTS_ERRORS, TELEMETRY_KNOWN_TESTS_RESPONSE_TESTS, - TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES, - getNumFromKnownTests + TELEMETRY_KNOWN_TESTS_RESPONSE_BYTES } = require('../../ci-visibility/telemetry') +const { getNumFromKnownTests } = require('../../plugins/util/test') + function getKnownTests ({ url, isEvpProxy, diff --git a/packages/dd-trace/src/plugins/util/test.js b/packages/dd-trace/src/plugins/util/test.js index 2748e9ab55b..6c0dde70cfb 100644 --- a/packages/dd-trace/src/plugins/util/test.js +++ b/packages/dd-trace/src/plugins/util/test.js @@ -623,6 +623,10 @@ function getTestSessionName (config, testCommand, envTags) { // 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)) {