Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test visibility] Fix num tests reported by EFD #4783

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
Loading