Skip to content

Commit

Permalink
fix(browser): print screenshot path alongside the test error message (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 28, 2024
1 parent b9fb92c commit 152891b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createBrowserRunner(

onTaskFinished = async (task: Task) => {
if (this.config.browser.screenshotFailures && task.result?.state === 'fail') {
await page.screenshot()
task.meta.failScreenshotPath = await page.screenshot()
}
}

Expand Down
18 changes: 14 additions & 4 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface PrintErrorOptions {
fullStack?: boolean
showCodeFrame?: boolean
printProperties?: boolean
screenshotPaths?: string[]
}

interface PrintErrorResult {
Expand Down Expand Up @@ -110,14 +111,19 @@ export function printError(
}
})

const errorProperties = printProperties
? getErrorProperties(e)
: {}

if (type) {
printErrorType(type, project.ctx)
}
printErrorMessage(e, logger)
if (options.screenshotPaths?.length) {
const length = options.screenshotPaths.length
logger.error(`\nFailure screenshot${length > 1 ? 's' : ''}:`)
logger.error(options.screenshotPaths.map(p => ` - ${c.dim(relative(process.cwd(), p))}`).join('\n'))
if (!e.diff) {
logger.error()
}
}

if (e.codeFrame) {
logger.error(`${e.codeFrame}\n`)
}
Expand All @@ -132,6 +138,10 @@ export function printError(
logger.error(c.yellow(e.frame))
}
else {
const errorProperties = printProperties
? getErrorProperties(e)
: {}

printStack(logger, project, stacks, nearest, errorProperties, (s) => {
if (showCodeFrame && s === nearest && nearest) {
const sourceCode = readFileSync(nearest.file, 'utf-8')
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ErrorOptions {
fullStack?: boolean
project?: WorkspaceProject
verbose?: boolean
screenshotPaths?: string[]
}

const ESC = '\x1B['
Expand Down Expand Up @@ -99,6 +100,7 @@ export class Logger {
showCodeFrame: true,
logger: this,
printProperties: options.verbose,
screenshotPaths: options.screenshotPaths,
})
}

Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,9 @@ export abstract class BaseReporter implements Reporter {
)}${name}`,
)
}
const screenshots = tasks.filter(t => t.meta?.failScreenshotPath).map(t => t.meta?.failScreenshotPath as string)
const project = this.ctx.getProjectByTaskId(tasks[0].id)
this.ctx.logger.printError(error, { project, verbose: this.verbose })
this.ctx.logger.printError(error, { project, verbose: this.verbose, screenshotPaths: screenshots })
errorDivider()
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ declare module '@vitest/runner' {
interface TaskMeta {
typecheck?: boolean
benchmark?: boolean
failScreenshotPath?: string
}

interface File {
Expand Down
2 changes: 2 additions & 0 deletions test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('running browser tests', async () => {
expect(stderr).toMatch(/- 2\s+\+ 1/)
expect(stderr).toContain('Expected to be')
expect(stderr).toContain('But got')
expect(stderr).toContain('Failure screenshot')
expect(stderr).toContain('__screenshots__/failing')
})

test('logs are redirected to stdout', () => {
Expand Down

0 comments on commit 152891b

Please sign in to comment.