Skip to content

Commit 4b126a0

Browse files
[test optimization] Allow keeping user coverage configuration even if TIA is enabled (#7700)
1 parent c8db939 commit 4b126a0

File tree

5 files changed

+138
-8
lines changed

5 files changed

+138
-8
lines changed

integration-tests/jest/jest.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,113 @@ describe(`jest@${JEST_VERSION} commonJS`, () => {
19451945
})
19461946
})
19471947

1948+
it('keeps user coverage reporters when DD_TEST_TIA_KEEP_COV_CONFIG is true', async () => {
1949+
receiver.setSettings({
1950+
itr_enabled: true,
1951+
code_coverage: true,
1952+
tests_skipping: true,
1953+
})
1954+
1955+
receiver.setSuitesToSkip([{
1956+
type: 'suite',
1957+
attributes: {
1958+
suite: 'ci-visibility/test/ci-visibility-test.js',
1959+
},
1960+
}])
1961+
1962+
const lcovPath = path.join(cwd, 'coverage', 'lcov.info')
1963+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
1964+
1965+
childProcess = exec(
1966+
runTestsCommand,
1967+
{
1968+
cwd,
1969+
env: {
1970+
...getCiVisAgentlessConfig(receiver.port),
1971+
COVERAGE_REPORTERS: 'lcov',
1972+
DD_TEST_TIA_KEEP_COV_CONFIG: 'true',
1973+
},
1974+
}
1975+
)
1976+
try {
1977+
await once(childProcess, 'exit')
1978+
assert.strictEqual(fs.existsSync(lcovPath), true)
1979+
} finally {
1980+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
1981+
}
1982+
})
1983+
1984+
it('overrides user coverage reporters when code coverage is enabled because of us', async () => {
1985+
receiver.setSettings({
1986+
itr_enabled: true,
1987+
code_coverage: true,
1988+
tests_skipping: true,
1989+
})
1990+
1991+
receiver.setSuitesToSkip([{
1992+
type: 'suite',
1993+
attributes: {
1994+
suite: 'ci-visibility/test/ci-visibility-test.js',
1995+
},
1996+
}])
1997+
1998+
const lcovPath = path.join(cwd, 'coverage', 'lcov.info')
1999+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
2000+
2001+
childProcess = exec(
2002+
runTestsCommand,
2003+
{
2004+
cwd,
2005+
env: {
2006+
...getCiVisAgentlessConfig(receiver.port),
2007+
COVERAGE_REPORTERS: 'lcov',
2008+
},
2009+
}
2010+
)
2011+
try {
2012+
await once(childProcess, 'exit')
2013+
assert.strictEqual(fs.existsSync(lcovPath), false)
2014+
} finally {
2015+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
2016+
}
2017+
})
2018+
2019+
it('keeps user coverage reporters when code coverage is enabled by the user', async () => {
2020+
receiver.setSettings({
2021+
itr_enabled: true,
2022+
code_coverage: true,
2023+
tests_skipping: true,
2024+
})
2025+
2026+
receiver.setSuitesToSkip([{
2027+
type: 'suite',
2028+
attributes: {
2029+
suite: 'ci-visibility/test/ci-visibility-test.js',
2030+
},
2031+
}])
2032+
2033+
const lcovPath = path.join(cwd, 'coverage', 'lcov.info')
2034+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
2035+
2036+
childProcess = exec(
2037+
runTestsCommand,
2038+
{
2039+
cwd,
2040+
env: {
2041+
...getCiVisAgentlessConfig(receiver.port),
2042+
ENABLE_CODE_COVERAGE: '1',
2043+
COVERAGE_REPORTERS: 'lcov',
2044+
},
2045+
}
2046+
)
2047+
try {
2048+
await once(childProcess, 'exit')
2049+
assert.strictEqual(fs.existsSync(lcovPath), true)
2050+
} finally {
2051+
fs.rmSync(path.join(cwd, 'coverage'), { recursive: true, force: true })
2052+
}
2053+
})
2054+
19482055
it('calculates executable lines even if there have been skipped suites', (done) => {
19492056
receiver.setSettings({
19502057
itr_enabled: true,

packages/datadog-instrumentations/src/jest.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ const RETRY_TIMES = Symbol.for('RETRY_TIMES')
6969
let skippableSuites = []
7070
let knownTests = {}
7171
let isCodeCoverageEnabled = false
72+
let isCodeCoverageEnabledBecauseOfUs = false
7273
let isSuitesSkippingEnabled = false
74+
let isKeepingCoverageConfiguration = false
7375
let isUserCodeCoverageEnabled = false
7476
let isSuitesSkipped = false
7577
let numSkippedSuites = 0
@@ -994,6 +996,8 @@ function getCliWrapper (isNewJestVersion) {
994996
if (!err) {
995997
isCodeCoverageEnabled = libraryConfig.isCodeCoverageEnabled
996998
isSuitesSkippingEnabled = libraryConfig.isSuitesSkippingEnabled
999+
isKeepingCoverageConfiguration =
1000+
libraryConfig.isKeepingCoverageConfiguration ?? isKeepingCoverageConfiguration
9971001
isEarlyFlakeDetectionEnabled = libraryConfig.isEarlyFlakeDetectionEnabled
9981002
earlyFlakeDetectionNumRetries = libraryConfig.earlyFlakeDetectionNumRetries
9991003
earlyFlakeDetectionSlowTestRetries = libraryConfig.earlyFlakeDetectionSlowTestRetries ?? {}
@@ -1327,9 +1331,10 @@ function coverageReporterWrapper (coverageReporter) {
13271331
*/
13281332
// `_addUntestedFiles` is an async function
13291333
shimmer.wrap(CoverageReporter.prototype, '_addUntestedFiles', addUntestedFiles => function () {
1330-
// If the user has added coverage manually, they're willing to pay the price of this execution, so
1331-
// we will not skip it.
1332-
if (isSuitesSkippingEnabled && !isUserCodeCoverageEnabled) {
1334+
if (isKeepingCoverageConfiguration) {
1335+
return addUntestedFiles.apply(this, arguments)
1336+
}
1337+
if (isCodeCoverageEnabledBecauseOfUs) {
13331338
return Promise.resolve()
13341339
}
13351340
return addUntestedFiles.apply(this, arguments)
@@ -1509,27 +1514,32 @@ function configureTestEnvironment (readConfigsResult) {
15091514
}
15101515

15111516
isUserCodeCoverageEnabled = !!readConfigsResult.globalConfig.collectCoverage
1517+
isCodeCoverageEnabledBecauseOfUs = isCodeCoverageEnabled && !isUserCodeCoverageEnabled
15121518

15131519
if (readConfigsResult.globalConfig.forceExit) {
15141520
log.warn("Jest's '--forceExit' flag has been passed. This may cause loss of data.")
15151521
}
15161522

1517-
if (isCodeCoverageEnabled) {
1523+
if (isCodeCoverageEnabledBecauseOfUs) {
15181524
const globalConfig = {
15191525
...readConfigsResult.globalConfig,
15201526
collectCoverage: true,
15211527
}
15221528
readConfigsResult.globalConfig = globalConfig
15231529
}
15241530
if (isSuitesSkippingEnabled) {
1525-
// If suite skipping is enabled, the code coverage results are not going to be relevant,
1526-
// so we do not show them.
1527-
// Also, we might skip every test, so we need to pass `passWithNoTests`
1531+
// If suite skipping is enabled, we pass `passWithNoTests` in case every test gets skipped.
15281532
const globalConfig = {
15291533
...readConfigsResult.globalConfig,
1530-
coverageReporters: ['none'],
15311534
passWithNoTests: true,
15321535
}
1536+
if (isCodeCoverageEnabledBecauseOfUs && !isKeepingCoverageConfiguration) {
1537+
globalConfig.coverageReporters = ['none']
1538+
readConfigsResult.configs = configs.map(config => ({
1539+
...config,
1540+
coverageReporters: ['none'],
1541+
}))
1542+
}
15331543
readConfigsResult.globalConfig = globalConfig
15341544
}
15351545

packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class CiVisibilityExporter extends BufferingExporter {
234234
testManagementAttemptToFixRetries ?? this._config.testManagementAttemptToFixRetries,
235235
isImpactedTestsEnabled: isImpactedTestsEnabled && this._config.isImpactedTestsEnabled,
236236
isCoverageReportUploadEnabled,
237+
isKeepingCoverageConfiguration: this._config.isKeepingCoverageConfiguration,
237238
}
238239
}
239240

packages/dd-trace/src/config/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class Config {
352352
DD_TELEMETRY_HEARTBEAT_INTERVAL,
353353
DD_TELEMETRY_LOG_COLLECTION_ENABLED,
354354
DD_TELEMETRY_METRICS_ENABLED,
355+
DD_TEST_TIA_KEEP_COV_CONFIG,
355356
DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED,
356357
DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED,
357358
DD_TRACE_AGENT_PORT,
@@ -757,6 +758,7 @@ class Config {
757758
unprocessedTarget['telemetry.heartbeatInterval'] = DD_TELEMETRY_HEARTBEAT_INTERVAL
758759
setBoolean(target, 'telemetry.logCollection', DD_TELEMETRY_LOG_COLLECTION_ENABLED)
759760
setBoolean(target, 'telemetry.metrics', DD_TELEMETRY_METRICS_ENABLED)
761+
setBoolean(target, 'isKeepingCoverageConfiguration', DD_TEST_TIA_KEEP_COV_CONFIG)
760762
setBoolean(target, 'traceId128BitGenerationEnabled', DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED)
761763
setBoolean(target, 'traceId128BitLoggingEnabled', DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED)
762764
warnIfPropagationStyleConflict(

packages/dd-trace/src/config/supported-configurations.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,16 @@
17701770
]
17711771
}
17721772
],
1773+
"DD_TEST_TIA_KEEP_COV_CONFIG": [
1774+
{
1775+
"implementation": "A",
1776+
"type": "boolean",
1777+
"default": "false",
1778+
"configurationNames": [
1779+
"isKeepingCoverageConfiguration"
1780+
]
1781+
}
1782+
],
17731783
"DD_TEST_SESSION_NAME": [
17741784
{
17751785
"implementation": "A",

0 commit comments

Comments
 (0)