diff --git a/packages/vitest/src/node/reporters/github-actions.ts b/packages/vitest/src/node/reporters/github-actions.ts index 8863bb7bbd50..ac751e45d6d8 100644 --- a/packages/vitest/src/node/reporters/github-actions.ts +++ b/packages/vitest/src/node/reporters/github-actions.ts @@ -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) { diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 6d1c5395bd29..f4df3b43b422 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -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 @@ -140,31 +139,6 @@ export class JUnitReporter implements Reporter { await this.logger.log(``) } - async writeErrorDetails(task: Task, error: ErrorWithDiff): Promise { - 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 { if (task.logs == null || task.logs.length === 0) return @@ -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()))) }) } }