Skip to content

Commit b4bd5c4

Browse files
[test optimization] Prevent ATF tests from being incorrectly tagged as new in jest (#7960)
1 parent 918b4d0 commit b4bd5c4

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

integration-tests/jest/jest.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5560,6 +5560,74 @@ describe(`jest@${JEST_VERSION} commonJS`, () => {
55605560
])
55615561
})
55625562

5563+
it('does not tag known attempt to fix tests as new', async () => {
5564+
receiver.setKnownTests({
5565+
jest: {
5566+
'ci-visibility/jest-flaky/flaky-fails.js': [
5567+
'test-flaky-test-retries can retry failed tests',
5568+
],
5569+
},
5570+
})
5571+
receiver.setSettings({
5572+
test_management: { enabled: true, attempt_to_fix_retries: 2 },
5573+
early_flake_detection: {
5574+
enabled: true,
5575+
slow_test_retries: {
5576+
'5s': 2,
5577+
},
5578+
faulty_session_threshold: 100,
5579+
},
5580+
known_tests_enabled: true,
5581+
})
5582+
5583+
receiver.setTestManagementTests({
5584+
jest: {
5585+
suites: {
5586+
'ci-visibility/jest-flaky/flaky-fails.js': {
5587+
tests: {
5588+
'test-flaky-test-retries can retry failed tests': {
5589+
properties: {
5590+
attempt_to_fix: true,
5591+
},
5592+
},
5593+
},
5594+
},
5595+
},
5596+
},
5597+
})
5598+
const eventsPromise = receiver
5599+
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
5600+
const events = payloads.flatMap(({ payload }) => payload.events)
5601+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
5602+
const atfTests = tests.filter(
5603+
t => t.meta[TEST_MANAGEMENT_IS_ATTEMPT_TO_FIX] === 'true'
5604+
)
5605+
assert.ok(atfTests.length > 0)
5606+
for (const test of atfTests) {
5607+
assert.ok(
5608+
!(TEST_IS_NEW in test.meta),
5609+
'ATF test that is in known tests should not be tagged as new'
5610+
)
5611+
}
5612+
})
5613+
5614+
childProcess = exec(
5615+
runTestsCommand,
5616+
{
5617+
cwd,
5618+
env: {
5619+
...getCiVisAgentlessConfig(receiver.port),
5620+
TESTS_TO_RUN: 'jest-flaky/flaky-fails.js',
5621+
},
5622+
}
5623+
)
5624+
5625+
await Promise.all([
5626+
once(childProcess, 'exit'),
5627+
eventsPromise,
5628+
])
5629+
})
5630+
55635631
it('resets mock state between attempt to fix retries', async () => {
55645632
const NUM_RETRIES = 3
55655633
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: NUM_RETRIES } })

packages/datadog-instrumentations/src/jest.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ const efdDeterminedRetries = new Map()
111111
const efdSlowAbortedTests = new Set()
112112
// Tests added as EFD new-test candidates (not ATF, not impacted).
113113
const efdNewTestCandidates = new Set()
114+
// Tests that are genuinely new (not in known tests list).
115+
const newTests = new Set()
114116
const testSuiteAbsolutePathsWithFastCheck = new Set()
115117
const testSuiteJestObjects = new Map()
116118

@@ -485,7 +487,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
485487
}
486488

487489
if (this.isKnownTestsEnabled) {
488-
isNewTest = retriedTestsToNumAttempts.has(testName)
490+
isNewTest = newTests.has(testName)
489491
}
490492

491493
const willRunEfd = this.isEarlyFlakeDetectionEnabled && (isNewTest || isModified)
@@ -605,6 +607,9 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
605607
}
606608
if (!isAttemptToFix && this.isKnownTestsEnabled) {
607609
const isNew = !this.knownTestsForThisSuite.includes(testFullName)
610+
if (isNew && !isSkipped) {
611+
newTests.add(testFullName)
612+
}
608613
if (isNew && !isSkipped && !retriedTestsToNumAttempts.has(testFullName)) {
609614
if (DYNAMIC_NAME_RE.test(testFullName)) {
610615
// Populated directly for runInBand; for parallel workers the main process
@@ -715,7 +720,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
715720
let isEfdRetry = false
716721
// We'll store the test statuses of the retries
717722
if (this.isKnownTestsEnabled) {
718-
const isNewTest = retriedTestsToNumAttempts.has(testName)
723+
const isNewTest = newTests.has(testName)
719724
if (isNewTest) {
720725
if (newTestsTestStatuses.has(testName)) {
721726
newTestsTestStatuses.get(testName).push(status)
@@ -811,6 +816,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) {
811816
efdDeterminedRetries.clear()
812817
efdSlowAbortedTests.clear()
813818
efdNewTestCandidates.clear()
819+
newTests.clear()
814820
retriedTestsToNumAttempts.clear()
815821
attemptToFixRetriedTestsStatuses.clear()
816822
testsToBeRetried.clear()

0 commit comments

Comments
 (0)