Skip to content

Commit 0b52fec

Browse files
[test optimization] Clean up handling of known tests in playwright (#6413)
1 parent 6472355 commit 0b52fec

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

integration-tests/playwright/playwright.spec.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { once } = require('node:events')
34
const { exec, execSync } = require('child_process')
45
const satisfies = require('semifies')
56
const path = require('path')
@@ -25,6 +26,7 @@ const {
2526
TEST_IS_NEW,
2627
TEST_IS_RETRY,
2728
TEST_EARLY_FLAKE_ENABLED,
29+
TEST_EARLY_FLAKE_ABORT_REASON,
2830
TEST_SUITE,
2931
TEST_CODE_OWNERS,
3032
TEST_SESSION_NAME,
@@ -537,7 +539,9 @@ versions.forEach((version) => {
537539
})
538540

539541
receiver.setKnownTestsResponseCode(500)
540-
receiver.setKnownTests({})
542+
receiver.setKnownTests({
543+
playwright: {}
544+
})
541545

542546
const receiverPromise = receiver
543547
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
@@ -641,6 +645,61 @@ versions.forEach((version) => {
641645
receiverPromise.then(() => done()).catch(done)
642646
})
643647
})
648+
649+
it('does not run EFD if the known tests response is invalid', async () => {
650+
receiver.setSettings({
651+
early_flake_detection: {
652+
enabled: true,
653+
slow_test_retries: {
654+
'5s': NUM_RETRIES_EFD
655+
}
656+
},
657+
known_tests_enabled: true
658+
})
659+
660+
receiver.setKnownTests(
661+
{
662+
'not-playwright': {}
663+
}
664+
)
665+
666+
const receiverPromise = receiver
667+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
668+
const events = payloads.flatMap(({ payload }) => payload.events)
669+
670+
const testSession = events.find(event => event.type === 'test_session_end').content
671+
assert.notProperty(testSession.meta, TEST_EARLY_FLAKE_ENABLED)
672+
assert.propertyVal(testSession.meta, TEST_EARLY_FLAKE_ABORT_REASON, 'faulty')
673+
674+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
675+
const newTests = tests.filter(test =>
676+
test.resource.endsWith('should work with passing tests')
677+
)
678+
newTests.forEach(test => {
679+
assert.notProperty(test.meta, TEST_IS_NEW)
680+
})
681+
682+
const retriedTests = tests.filter(test => test.meta[TEST_IS_RETRY] === 'true')
683+
assert.equal(retriedTests.length, 0)
684+
})
685+
686+
childProcess = exec(
687+
'./node_modules/.bin/playwright test -c playwright.config.js',
688+
{
689+
cwd,
690+
env: {
691+
...getCiVisAgentlessConfig(receiver.port),
692+
PW_BASE_URL: `http://localhost:${webAppPort}`
693+
},
694+
stdio: 'pipe'
695+
}
696+
)
697+
698+
await Promise.all([
699+
once(childProcess, 'exit'),
700+
receiverPromise,
701+
])
702+
})
644703
})
645704

646705
it('does not crash when maxFailures=1 and there is an error', (done) => {
@@ -1703,7 +1762,9 @@ versions.forEach((version) => {
17031762

17041763
context('test is new', () => {
17051764
it('should be retried and marked both as new and modified', (done) => {
1706-
receiver.setKnownTests({})
1765+
receiver.setKnownTests({
1766+
playwright: {}
1767+
})
17071768
receiver.setSettings({
17081769
impacted_tests_enabled: true,
17091770
early_flake_detection: {

packages/datadog-instrumentations/src/playwright.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ let remainingTestsByFile = {}
5151
let isKnownTestsEnabled = false
5252
let isEarlyFlakeDetectionEnabled = false
5353
let earlyFlakeDetectionNumRetries = 0
54+
let isEarlyFlakeDetectionFaulty = false
5455
let isFlakyTestRetriesEnabled = false
5556
let flakyTestRetriesCount = 0
5657
let knownTests = {}
@@ -64,6 +65,10 @@ let quarantinedButNotAttemptToFixFqns = new Set()
6465
let rootDir = ''
6566
const MINIMUM_SUPPORTED_VERSION_RANGE_EFD = '>=1.38.0' // TODO: remove this once we drop support for v5
6667

68+
function isValidKnownTests (receivedKnownTests) {
69+
return !!receivedKnownTests.playwright
70+
}
71+
6772
function getTestFullyQualifiedName (test) {
6873
const fullname = getTestFullname(test)
6974
return `${test._requireFile} ${fullname}`
@@ -79,8 +84,11 @@ function getTestProperties (test) {
7984
}
8085

8186
function isNewTest (test) {
87+
if (!isValidKnownTests(knownTests)) {
88+
return false
89+
}
8290
const testSuite = getTestSuitePath(test._requireFile, rootDir)
83-
const testsForSuite = knownTests?.playwright?.[testSuite] || []
91+
const testsForSuite = knownTests.playwright[testSuite] || []
8492

8593
return !testsForSuite.includes(getTestFullname(test))
8694
}
@@ -557,6 +565,11 @@ function runAllTestsWrapper (runAllTests, playwrightVersion) {
557565
} else {
558566
knownTests = receivedKnownTests
559567
}
568+
if (!isValidKnownTests(receivedKnownTests)) {
569+
isEarlyFlakeDetectionFaulty = true
570+
isEarlyFlakeDetectionEnabled = false
571+
isKnownTestsEnabled = false
572+
}
560573
} catch (err) {
561574
isEarlyFlakeDetectionEnabled = false
562575
isKnownTestsEnabled = false
@@ -652,6 +665,7 @@ function runAllTestsWrapper (runAllTests, playwrightVersion) {
652665
testSessionFinishCh.publish({
653666
status: STATUS_TO_TEST_STATUS[sessionStatus],
654667
isEarlyFlakeDetectionEnabled,
668+
isEarlyFlakeDetectionFaulty,
655669
isTestManagementTestsEnabled,
656670
onDone
657671
})

packages/datadog-plugin-playwright/src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
TEST_IS_NEW,
1818
TEST_IS_RETRY,
1919
TEST_EARLY_FLAKE_ENABLED,
20+
TEST_EARLY_FLAKE_ABORT_REASON,
2021
TELEMETRY_TEST_SESSION,
2122
TEST_RETRY_REASON,
2223
TEST_MANAGEMENT_IS_QUARANTINED,
@@ -70,6 +71,7 @@ class PlaywrightPlugin extends CiPlugin {
7071
this.addSub('ci:playwright:session:finish', ({
7172
status,
7273
isEarlyFlakeDetectionEnabled,
74+
isEarlyFlakeDetectionFaulty,
7375
isTestManagementTestsEnabled,
7476
onDone
7577
}) => {
@@ -79,7 +81,9 @@ class PlaywrightPlugin extends CiPlugin {
7981
if (isEarlyFlakeDetectionEnabled) {
8082
this.testSessionSpan.setTag(TEST_EARLY_FLAKE_ENABLED, 'true')
8183
}
82-
84+
if (isEarlyFlakeDetectionFaulty) {
85+
this.testSessionSpan.setTag(TEST_EARLY_FLAKE_ABORT_REASON, 'faulty')
86+
}
8387
if (this.numFailedSuites > 0) {
8488
let errorMessage = `Test suites failed: ${this.numFailedSuites}.`
8589
if (this.numFailedTests > 0) {

0 commit comments

Comments
 (0)