Skip to content

Commit

Permalink
[test visibility] Fix num tests reported by EFD (#4783)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored Oct 16, 2024
1 parent 501ff2f commit fd0f570
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion packages/dd-trace/src/plugins/util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
32 changes: 31 additions & 1 deletion packages/dd-trace/test/plugins/util/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
})
})

0 comments on commit fd0f570

Please sign in to comment.