From 46383d3d0f6f4aad6a6010ce9663cf7648f3953a Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:07:04 +0900 Subject: [PATCH 1/4] fix(reporter/junit): use same error formatting as default reporter --- packages/vitest/src/node/reporters/github-actions.ts | 2 +- packages/vitest/src/node/reporters/junit.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) 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..bf6f24dd25fa 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -11,6 +11,7 @@ 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 @@ -205,7 +206,13 @@ 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), + ) + // TODO: should strip color? + await this.baseLog(escapeXML(result.output)) }) } } From 4d035ac9656cd3a695ebe72e79fe3245dd66e8d4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:16:24 +0900 Subject: [PATCH 2/4] chore: strip color --- packages/vitest/src/node/reporters/junit.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index bf6f24dd25fa..30c7f3b6f86c 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -5,6 +5,7 @@ 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' @@ -211,8 +212,7 @@ export class JUnitReporter implements Reporter { this.ctx, this.ctx.getProjectByTaskId(task.id), ) - // TODO: should strip color? - await this.baseLog(escapeXML(result.output)) + await this.baseLog(escapeXML(stripAnsi(result.output))) }) } } From 10e707cc68d20282507f9f5b43ff0701ca26cd70 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:24:04 +0900 Subject: [PATCH 3/4] chore: trim --- packages/vitest/src/node/reporters/junit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 30c7f3b6f86c..650cc5937320 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -212,7 +212,7 @@ export class JUnitReporter implements Reporter { this.ctx, this.ctx.getProjectByTaskId(task.id), ) - await this.baseLog(escapeXML(stripAnsi(result.output))) + await this.baseLog(escapeXML(stripAnsi(result.output.trim()))) }) } } From ed781d54322f873b7be71019da753d50def9cf11 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:26:01 +0900 Subject: [PATCH 4/4] chore: remove unused --- packages/vitest/src/node/reporters/junit.ts | 28 --------------------- 1 file changed, 28 deletions(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 650cc5937320..f4df3b43b422 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -3,13 +3,10 @@ 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' @@ -142,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