Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reporter): use default error formatter for JUnit #5626

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class GithubActionsReporter implements Reporter {

// use Logger with custom Console to extract messgage from `processError` util
// TODO: maybe refactor `processError` to require single function `(message: string) => void` instead of full Logger?
async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) {
export async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) {
let output = ''
const writable = new Writable({
write(chunk, _encoding, callback) {
Expand Down
37 changes: 8 additions & 29 deletions packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { hostname } from 'node:os'
import { dirname, relative, resolve } from 'pathe'

import type { Task } from '@vitest/runner'
import type { ErrorWithDiff } from '@vitest/utils'
import { getSuites } from '@vitest/runner/utils'
import stripAnsi from 'strip-ansi'
import type { Vitest } from '../../node'
import type { Reporter } from '../../types/reporter'
import { parseErrorStacktrace } from '../../utils/source-map'
import { F_POINTER } from '../../utils/figures'
import { getOutputFile } from '../../utils/config-helpers'
import { IndentedLogger } from './renderers/indented-logger'
import { printErrorWrapper } from './github-actions'

export interface JUnitOptions {
outputFile?: string
Expand Down Expand Up @@ -140,31 +139,6 @@ export class JUnitReporter implements Reporter {
await this.logger.log(`</${name}>`)
}

async writeErrorDetails(task: Task, error: ErrorWithDiff): Promise<void> {
const errorName = error.name ?? error.nameStr ?? 'Unknown Error'
const errorDetails = `${errorName}: ${error.message}`

// Be sure to escape any XML in the error Details
await this.baseLog(escapeXML(errorDetails))

const project = this.ctx.getProjectByTaskId(task.id)
const stack = parseErrorStacktrace(error, {
getSourceMap: file => project.getBrowserSourceMapModuleById(file),
frameFilter: this.ctx.config.onStackTrace,
})

// TODO: This is same as printStack but without colors. Find a way to reuse code.
for (const frame of stack) {
const path = relative(this.ctx.config.root, frame.file)

await this.baseLog(escapeXML(` ${F_POINTER} ${[frame.method, `${path}:${frame.line}:${frame.column}`].filter(Boolean).join(' ')}`))

// reached at test file, skip the follow stack
if (frame.file in this.ctx.state.filesMap)
break
}
}

async writeLogs(task: Task, type: 'err' | 'out'): Promise<void> {
if (task.logs == null || task.logs.length === 0)
return
Expand Down Expand Up @@ -205,7 +179,12 @@ export class JUnitReporter implements Reporter {
if (!error)
return

await this.writeErrorDetails(task, error)
const result = await printErrorWrapper(
error,
this.ctx,
this.ctx.getProjectByTaskId(task.id),
)
await this.baseLog(escapeXML(stripAnsi(result.output.trim())))
Copy link
Contributor Author

@hi-ogawa hi-ogawa Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stripped the color for now, but it would be cool to have diff color if we can expect many consumers of junit reporter supports ansi code on their UI.
From the OP's screenshot, CircleCI probably supports it.

})
}
}
Expand Down
Loading