Skip to content

Commit 40b1880

Browse files
[test optimization] Playwright: report tests that did not run (#6797)
1 parent 3522ff7 commit 40b1880

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const { test, expect } = require('@playwright/test')
4+
5+
test.describe('did not run', () => {
6+
test('because of early bail', async () => {
7+
expect(true).toBe(false)
8+
})
9+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const { test, expect } = require('@playwright/test')
4+
5+
test.describe('failing test', () => {
6+
test('fails and causes early bail', async () => {
7+
expect(true).toBe(false)
8+
})
9+
})

integration-tests/playwright.config.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
// Playwright config file for integration tests
44
const { devices } = require('@playwright/test')
55

6+
const projects = [
7+
{
8+
name: 'chromium',
9+
use: {
10+
...devices['Desktop Chrome']
11+
}
12+
}
13+
]
14+
15+
if (process.env.ADD_EXTRA_PLAYWRIGHT_PROJECT) {
16+
projects.push({
17+
name: 'extra-project',
18+
use: {
19+
...devices['Desktop Chrome'],
20+
},
21+
dependencies: ['chromium'],
22+
testMatch: 'did-not-run.js'
23+
})
24+
}
25+
626
const config = {
727
baseURL: process.env.PW_BASE_URL,
828
testDir: process.env.TEST_DIR || './ci-visibility/playwright-tests',
@@ -11,14 +31,7 @@ const config = {
1131
workers: process.env.PLAYWRIGHT_WORKERS ? Number(process.env.PLAYWRIGHT_WORKERS) : undefined,
1232
reporter: 'line',
1333
/* Configure projects for major browsers */
14-
projects: [
15-
{
16-
name: 'chromium',
17-
use: {
18-
...devices['Desktop Chrome']
19-
}
20-
}
21-
],
34+
projects,
2235
testMatch: '**/*-test.js'
2336
}
2437

integration-tests/playwright/playwright.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,5 +2070,39 @@ versions.forEach((version) => {
20702070
})
20712071
})
20722072
})
2073+
2074+
contextNewVersions('playwright early bail', () => {
2075+
it('reports tests that did not run', async () => {
2076+
const receiverPromise = receiver
2077+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
2078+
const events = payloads.flatMap(({ payload }) => payload.events)
2079+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
2080+
assert.equal(tests.length, 2)
2081+
const failedTest = tests.find(test => test.meta[TEST_STATUS] === 'fail')
2082+
assert.propertyVal(failedTest.meta, TEST_NAME, 'failing test fails and causes early bail')
2083+
const didNotRunTest = tests.find(test => test.meta[TEST_STATUS] === 'skip')
2084+
assert.propertyVal(didNotRunTest.meta, TEST_NAME, 'did not run because of early bail')
2085+
})
2086+
2087+
childProcess = exec(
2088+
'./node_modules/.bin/playwright test -c playwright.config.js',
2089+
{
2090+
cwd,
2091+
env: {
2092+
...getCiVisAgentlessConfig(receiver.port),
2093+
PW_BASE_URL: `http://localhost:${webAppPort}`,
2094+
TEST_DIR: './ci-visibility/playwright-did-not-run',
2095+
ADD_EXTRA_PLAYWRIGHT_PROJECT: 'true'
2096+
},
2097+
stdio: 'pipe'
2098+
}
2099+
)
2100+
2101+
await Promise.all([
2102+
once(childProcess, 'exit'),
2103+
receiverPromise
2104+
])
2105+
})
2106+
})
20732107
})
20742108
})

packages/datadog-instrumentations/src/playwright.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ let modifiedFiles = {}
6868
const quarantinedOrDisabledTestsAttemptToFix = []
6969
let quarantinedButNotAttemptToFixFqns = new Set()
7070
let rootDir = ''
71+
let sessionProjects = []
72+
7173
const MINIMUM_SUPPORTED_VERSION_RANGE_EFD = '>=1.38.0' // TODO: remove this once we drop support for v5
7274

7375
function isValidKnownTests (receivedKnownTests) {
@@ -495,6 +497,7 @@ function dispatcherHook (dispatcherExport) {
495497
const dispatcher = this
496498
const worker = createWorker.apply(this, arguments)
497499
const projects = getProjectsFromDispatcher(dispatcher)
500+
sessionProjects = projects
498501

499502
// for older versions of playwright, `shouldCreateTestSpan` should always be true,
500503
// since the `_runTest` function wrapper is not available for older versions
@@ -535,6 +538,7 @@ function dispatcherHookNew (dispatcherExport, runWrapper) {
535538
const dispatcher = this
536539
const worker = createWorker.apply(this, arguments)
537540
const projects = getProjectsFromDispatcher(dispatcher)
541+
sessionProjects = projects
538542

539543
worker.on('testBegin', ({ testId }) => {
540544
const test = getTestByTestId(dispatcher, testId)
@@ -1255,3 +1259,46 @@ addHook({
12551259

12561260
return workerPackage
12571261
})
1262+
1263+
function generateSummaryWrapper (generateSummary) {
1264+
return function () {
1265+
for (const test of this.suite.allTests()) {
1266+
// https://github.com/microsoft/playwright/blob/bf92ffecff6f30a292b53430dbaee0207e0c61ad/packages/playwright/src/reporters/base.ts#L279
1267+
const didNotRun = test.outcome() === 'skipped' &&
1268+
(!test.results.length || test.expectedStatus !== 'skipped')
1269+
if (didNotRun) {
1270+
const {
1271+
_requireFile: testSuiteAbsolutePath,
1272+
location: { line: testSourceLine },
1273+
} = test
1274+
const browserName = getBrowserNameFromProjects(sessionProjects, test)
1275+
1276+
testSkipCh.publish({
1277+
testName: getTestFullname(test),
1278+
testSuiteAbsolutePath,
1279+
testSourceLine,
1280+
browserName,
1281+
})
1282+
}
1283+
}
1284+
return generateSummary.apply(this, arguments)
1285+
}
1286+
}
1287+
1288+
// If a playwright project B has a dependency on project A,
1289+
// and project A fails, the tests in project B will not run.
1290+
// This hook is used to report tests that did not run as skipped.
1291+
// Note: this is different from tests skipped via test.skip() or test.fixme()
1292+
addHook({
1293+
name: 'playwright',
1294+
file: 'lib/reporters/base.js',
1295+
versions: ['>=1.38.0']
1296+
}, (reportersPackage) => {
1297+
// v1.50.0 changed the name of the base reporter from BaseReporter to TerminalReporter
1298+
if (reportersPackage.TerminalReporter) {
1299+
shimmer.wrap(reportersPackage.TerminalReporter.prototype, 'generateSummary', generateSummaryWrapper)
1300+
} else if (reportersPackage.BaseReporter) {
1301+
shimmer.wrap(reportersPackage.BaseReporter.prototype, 'generateSummary', generateSummaryWrapper)
1302+
}
1303+
return reportersPackage
1304+
})

0 commit comments

Comments
 (0)