diff --git a/packages/vitest/src/node/logger.ts b/packages/vitest/src/node/logger.ts index d42907286..fcae0950f 100644 --- a/packages/vitest/src/node/logger.ts +++ b/packages/vitest/src/node/logger.ts @@ -1,13 +1,13 @@ -import type { Task } from '@vitest/runner' import type { Writable } from 'node:stream' import type { TypeCheckError } from '../typecheck/typechecker' import type { Vitest } from './core' +import type { CapturePrintErrorResult } from './printError' import type { TestProject } from './project' import { Console } from 'node:console' import { toArray } from '@vitest/utils/helpers' import c from 'tinyrainbow' import { highlightCode } from '../utils/colors' -import { printError } from './printError' +import { capturePrintError, printError } from './printError' import { divider, errorBanner, formatProjectName, withLabel } from './reporters/renderers/utils' import { RandomSequencer } from './sequencers/RandomSequencer' @@ -17,7 +17,6 @@ export interface ErrorOptions { project?: TestProject verbose?: boolean screenshotPaths?: string[] - task?: Task showCodeFrame?: boolean } @@ -109,6 +108,10 @@ export class Logger { printError(err, this.ctx, this, options) } + formatError(err: unknown, options: ErrorOptions = {}): CapturePrintErrorResult { + return capturePrintError(err, this.ctx, options) + } + deprecate(message: string): void { this.error(c.bold(c.bgYellow(' DEPRECATED ')), c.yellow(message)) } diff --git a/packages/vitest/src/node/printError.ts b/packages/vitest/src/node/printError.ts index edd70ac88..3fc6f50ae 100644 --- a/packages/vitest/src/node/printError.ts +++ b/packages/vitest/src/node/printError.ts @@ -36,12 +36,17 @@ interface PrintErrorResult { nearest?: ParsedStack } +export interface CapturePrintErrorResult { + nearest: ParsedStack | undefined + output: string +} + // use Logger with custom Console to capture entire error printing export function capturePrintError( error: unknown, ctx: Vitest, options: ErrorOptions, -): { nearest: ParsedStack | undefined; output: string } { +): CapturePrintErrorResult { let output = '' const writable = new Writable({ write(chunk, _encoding, callback) { @@ -90,7 +95,7 @@ export function printError( // browser stack trace needs to be processed differently, // so there is a separate method for that - if (options.task?.file.pool === 'browser' && project.browser) { + if (project.browser) { return project.browser.parseErrorStacktrace(error, { frameFilter: project.config.onStackTrace, ignoreStackEntries: options.fullStack ? [] : undefined, diff --git a/packages/vitest/src/node/reporters/base.ts b/packages/vitest/src/node/reporters/base.ts index 132a83a9d..b1543f1f0 100644 --- a/packages/vitest/src/node/reporters/base.ts +++ b/packages/vitest/src/node/reporters/base.ts @@ -965,7 +965,6 @@ export abstract class BaseReporter implements Reporter { project: this.ctx.getProjectByName(tasks[0].file.projectName || ''), verbose: this.verbose, screenshotPaths, - task: tasks[0], }) if (tasks[0].type === 'test' && tasks[0].annotations.length) { diff --git a/packages/vitest/src/node/reporters/github-actions.ts b/packages/vitest/src/node/reporters/github-actions.ts index e341b944d..d5ea9d02b 100644 --- a/packages/vitest/src/node/reporters/github-actions.ts +++ b/packages/vitest/src/node/reporters/github-actions.ts @@ -1,4 +1,4 @@ -import type { File, TestAnnotation } from '@vitest/runner' +import type { TestAnnotation } from '@vitest/runner' import type { SerializedError } from '@vitest/utils' import type { Vitest } from '../core' import type { TestProject } from '../project' @@ -9,7 +9,6 @@ import { stripVTControlCharacters } from 'node:util' import { getFullName, getTasks } from '@vitest/runner/utils' import { deepMerge } from '@vitest/utils/helpers' import { relative } from 'pathe' -import { capturePrintError } from '../printError' import { noun } from './renderers/utils' export interface GithubActionsReporterOptions { @@ -127,7 +126,6 @@ export class GithubActionsReporter implements Reporter { project: TestProject title: string error: unknown - file?: File }>() for (const error of errors) { projectErrors.push({ @@ -150,15 +148,14 @@ export class GithubActionsReporter implements Reporter { project, title: project.name ? `[${project.name}] ${title}` : title, error, - file, }) } } } // format errors via `printError` - for (const { project, title, error, file } of projectErrors) { - const result = capturePrintError(error, this.ctx, { project, task: file }) + for (const { project, title, error } of projectErrors) { + const result = this.ctx.logger.formatError(error, { project }) const stack = result?.nearest if (!stack) { continue diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 7d51bc611..34817666a 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -9,7 +9,6 @@ import { stripVTControlCharacters } from 'node:util' import { getSuites } from '@vitest/runner/utils' import { basename, dirname, relative, resolve } from 'pathe' import { getOutputFile } from '../../utils/config-helpers' -import { capturePrintError } from '../printError' import { IndentedLogger } from './renderers/indented-logger' export interface ClassnameTemplateVariables { @@ -396,11 +395,9 @@ export class JUnitReporter implements Reporter { return } - const result = capturePrintError( - error, - this.ctx, - { project: this.ctx.getProjectByName(task.file?.projectName ?? ''), task }, - ) + const result = this.ctx.logger.formatError(error, { + project: this.ctx.getProjectByName(task.file?.projectName ?? ''), + }) await this.baseLog( escapeXML(stripVTControlCharacters(result.output.trim())), )