Skip to content

Commit 453f900

Browse files
[test optimization] Fix attempt to fix in vitest (#7785)
1 parent 545939d commit 453f900

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

integration-tests/ci-visibility/vitest-tests/test-attempt-to-fix.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ describe('attempt to fix tests', () => {
1515
} else {
1616
expect(1 + 2).to.equal(3)
1717
}
18+
} else if (process.env.SHOULD_FAIL_FIRST_ONLY) {
19+
// First attempt fails, all retries pass. Exit code must still be 1
20+
// for plain ATF tests (not quarantined/disabled).
21+
if (numAttempt++ === 0) {
22+
expect(1 + 2).to.equal(4)
23+
} else {
24+
expect(1 + 2).to.equal(3)
25+
}
1826
} else {
1927
expect(1 + 2).to.equal(4)
2028
}

integration-tests/vitest/vitest.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,7 @@ versions.forEach((version) => {
17161716
shouldAlwaysPass,
17171717
isQuarantining,
17181718
shouldFailSometimes,
1719+
shouldFailFirstOnly,
17191720
isDisabling,
17201721
extraEnvVars = {},
17211722
} = {}) => {
@@ -1738,6 +1739,7 @@ versions.forEach((version) => {
17381739
...extraEnvVars,
17391740
...(shouldAlwaysPass ? { SHOULD_ALWAYS_PASS: '1' } : {}),
17401741
...(shouldFailSometimes ? { SHOULD_FAIL_SOMETIMES: '1' } : {}),
1742+
...(shouldFailFirstOnly ? { SHOULD_FAIL_FIRST_ONLY: '1' } : {}),
17411743
},
17421744
}
17431745
)
@@ -1777,6 +1779,12 @@ versions.forEach((version) => {
17771779
runAttemptToFixTest(done, { isAttemptingToFix: true, shouldFailSometimes: true })
17781780
})
17791781

1782+
it('does not suppress exit code for plain ATF tests even when last retry passes', (done) => {
1783+
receiver.setSettings({ test_management: { enabled: true, attempt_to_fix_retries: 3 } })
1784+
1785+
runAttemptToFixTest(done, { isAttemptingToFix: true, shouldFailFirstOnly: true })
1786+
})
1787+
17801788
it('does not attempt to fix tests if test management is not enabled', (done) => {
17811789
receiver.setSettings({ test_management: { enabled: false, attempt_to_fix_retries: 3 } })
17821790

packages/datadog-instrumentations/src/vitest.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,10 @@ function wrapVitestTestRunner (VitestTestRunner) {
747747
}
748748

749749
const lastExecutionStatus = task.result.state
750-
const shouldFlipStatus = isEarlyFlakeDetectionEnabled || attemptToFixTasks.has(task)
750+
const isAtf = attemptToFixTasks.has(task)
751+
const isQuarantinedOrDisabledAtf = isAtf && (quarantinedTasks.has(task) || disabledTasks.has(task))
752+
const shouldTrackStatuses = isEarlyFlakeDetectionEnabled || isAtf
753+
const shouldFlipStatus = isEarlyFlakeDetectionEnabled || isQuarantinedOrDisabledAtf
751754
const statuses = taskToStatuses.get(task)
752755

753756
// These clauses handle task.repeats, whether EFD is enabled or not
@@ -765,8 +768,10 @@ function wrapVitestTestRunner (VitestTestRunner) {
765768
} else {
766769
testPassCh.publish({ task, ...ctx.currentStore })
767770
}
768-
if (shouldFlipStatus) {
771+
if (shouldTrackStatuses) {
769772
statuses.push(lastExecutionStatus)
773+
}
774+
if (shouldFlipStatus) {
770775
// If we don't "reset" the result.state to "pass", once a repetition fails,
771776
// vitest will always consider the test as failed, so we can't read the actual status
772777
// This means that we change vitest's behavior:
@@ -776,7 +781,7 @@ function wrapVitestTestRunner (VitestTestRunner) {
776781
}
777782
}
778783
} else if (numRepetition === task.repeats) {
779-
if (shouldFlipStatus) {
784+
if (shouldTrackStatuses) {
780785
statuses.push(lastExecutionStatus)
781786
}
782787

0 commit comments

Comments
 (0)