Skip to content

Commit

Permalink
test(runner): preserve browser tracing if test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Dec 10, 2023
1 parent e02fe31 commit b4c854f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 39 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ jobs:
with:
name: test-reports
path: |
test/traces
test/test-junit-report
test/turbopack-test-junit-report
if-no-files-found: ignore
35 changes: 22 additions & 13 deletions run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ ${ENDGROUP}`)

child.on('exit', async (code, signal) => {
children.delete(child)
if (code !== 0 || signal !== null) {
const isChildExitWithNonZero = code !== 0 || signal !== null
if (isChildExitWithNonZero) {
if (hideOutput) {
await outputSema.acquire()
const isExpanded =
Expand Down Expand Up @@ -550,18 +551,26 @@ ${ENDGROUP}`)

return reject(err)
}
await fsp
.rm(
path.join(
__dirname,
'test/traces',
path
.relative(path.join(__dirname, 'test'), test.file)
.replace(/\//g, '-')
),
{ recursive: true, force: true }
)
.catch(() => {})

// If environment is CI and if this test execution is failed after retry, preserve test traces
// to upload into github actions artifacts for debugging purpose
const shouldPreserveTracesOutput =
process.env.CI && isRetry && isChildExitWithNonZero
if (!shouldPreserveTracesOutput) {
await fsp
.rm(
path.join(
__dirname,
'test/traces',
path
.relative(path.join(__dirname, 'test'), test.file)
.replace(/\//g, '-')
),
{ recursive: true, force: true }
)
.catch(() => {})
}

resolve(new Date().getTime() - start)
})
})
Expand Down
93 changes: 68 additions & 25 deletions test/lib/browsers/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,65 @@ export class Playwright extends BrowserInterface {
request: new Set(),
}

private async initContextTracing(
url: string,
page: Page,
context: BrowserContext
) {
if (!tracePlaywright) {
return
}

try {
await this.teardownTracing()
await context.tracing.start({
screenshots: true,
snapshots: true,
})
this.activeTrace = encodeURIComponent(url)

page.on('close', async () => {
await this.teardownTracing()
})
} catch (e) {
require('console').warn('failed to start playwright trace', e)
this.activeTrace = undefined
}
}

private async teardownTracing() {
if (!tracePlaywright || !this.activeTrace) {
return
}

try {
const traceDir = path.join(__dirname, '../../traces')
const traceOutputPath = path.join(
traceDir,
`${path
.relative(path.join(__dirname, '../../'), process.env.TEST_FILE_PATH)
.replace(/\//g, '-')}`,
`playwright-${this.activeTrace}-${Date.now()}.zip`
)

await fs.remove(traceOutputPath)
await context.tracing
.stop({
path: traceOutputPath,
})
.catch((err) => console.error('failed to write playwright trace', err))

require('console').error('activeTrace', {
activeTrace: this.activeTrace,
traceOutputPath,
})
} catch (e) {
require('console').warn(e)
} finally {
this.activeTrace = undefined
}
}

on(event: Event, cb: (...args: any[]) => void) {
if (!this.eventCallbacks[event]) {
throw new Error(
Expand Down Expand Up @@ -82,6 +141,9 @@ export class Playwright extends BrowserInterface {
ignoreHTTPSErrors,
...device,
})
context.once('close', async () => {
await this.teardownTracing()
})
contextHasJSEnabled = javaScriptEnabled
}
return
Expand All @@ -94,6 +156,9 @@ export class Playwright extends BrowserInterface {
ignoreHTTPSErrors,
...device,
})
context.once('close', async () => {
await this.teardownTracing()
})
contextHasJSEnabled = javaScriptEnabled
}

Expand Down Expand Up @@ -134,30 +199,15 @@ export class Playwright extends BrowserInterface {
beforePageLoad?: (...args: any[]) => void
}
) {
if (this.activeTrace) {
const traceDir = path.join(__dirname, '../../traces')
const traceOutputPath = path.join(
traceDir,
`${path
.relative(path.join(__dirname, '../../'), process.env.TEST_FILE_PATH)
.replace(/\//g, '-')}`,
`playwright-${this.activeTrace}-${Date.now()}.zip`
)

await fs.remove(traceOutputPath)
await context.tracing
.stop({
path: traceOutputPath,
})
.catch((err) => console.error('failed to write playwright trace', err))
}

// clean-up existing pages
for (const oldPage of context.pages()) {
await oldPage.close()
}

page = await context.newPage()

this.initContextTracing(url, page, context)

// in development compilation can take longer due to
// lower CPU availability in GH actions
page.setDefaultTimeout(60 * 1000)
Expand Down Expand Up @@ -219,13 +269,6 @@ export class Playwright extends BrowserInterface {

opts?.beforePageLoad?.(page)

if (tracePlaywright) {
await context.tracing.start({
screenshots: true,
snapshots: true,
})
this.activeTrace = encodeURIComponent(url)
}
await page.goto(url, { waitUntil: 'load' })
}

Expand Down
4 changes: 3 additions & 1 deletion test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ export class NextInstance {
`next-trace`
)
)
.catch(() => {})
.catch((e) => {
require('console').error(e)
})
}

if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
Expand Down

0 comments on commit b4c854f

Please sign in to comment.