Skip to content

Commit 2c5f3ee

Browse files
hi-ogawacodex
andauthored
feat: add logger.formatError (#10268)
Co-authored-by: Codex <noreply@openai.com>
1 parent d22b029 commit 2c5f3ee

5 files changed

Lines changed: 19 additions & 18 deletions

File tree

packages/vitest/src/node/logger.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { Task } from '@vitest/runner'
21
import type { Writable } from 'node:stream'
32
import type { TypeCheckError } from '../typecheck/typechecker'
43
import type { Vitest } from './core'
4+
import type { CapturePrintErrorResult } from './printError'
55
import type { TestProject } from './project'
66
import { Console } from 'node:console'
77
import { toArray } from '@vitest/utils/helpers'
88
import c from 'tinyrainbow'
99
import { highlightCode } from '../utils/colors'
10-
import { printError } from './printError'
10+
import { capturePrintError, printError } from './printError'
1111
import { divider, errorBanner, formatProjectName, withLabel } from './reporters/renderers/utils'
1212
import { RandomSequencer } from './sequencers/RandomSequencer'
1313

@@ -17,7 +17,6 @@ export interface ErrorOptions {
1717
project?: TestProject
1818
verbose?: boolean
1919
screenshotPaths?: string[]
20-
task?: Task
2120
showCodeFrame?: boolean
2221
}
2322

@@ -109,6 +108,10 @@ export class Logger {
109108
printError(err, this.ctx, this, options)
110109
}
111110

111+
formatError(err: unknown, options: ErrorOptions = {}): CapturePrintErrorResult {
112+
return capturePrintError(err, this.ctx, options)
113+
}
114+
112115
deprecate(message: string): void {
113116
this.error(c.bold(c.bgYellow(' DEPRECATED ')), c.yellow(message))
114117
}

packages/vitest/src/node/printError.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ interface PrintErrorResult {
3636
nearest?: ParsedStack
3737
}
3838

39+
export interface CapturePrintErrorResult {
40+
nearest: ParsedStack | undefined
41+
output: string
42+
}
43+
3944
// use Logger with custom Console to capture entire error printing
4045
export function capturePrintError(
4146
error: unknown,
4247
ctx: Vitest,
4348
options: ErrorOptions,
44-
): { nearest: ParsedStack | undefined; output: string } {
49+
): CapturePrintErrorResult {
4550
let output = ''
4651
const writable = new Writable({
4752
write(chunk, _encoding, callback) {
@@ -90,7 +95,7 @@ export function printError(
9095

9196
// browser stack trace needs to be processed differently,
9297
// so there is a separate method for that
93-
if (options.task?.file.pool === 'browser' && project.browser) {
98+
if (project.browser) {
9499
return project.browser.parseErrorStacktrace(error, {
95100
frameFilter: project.config.onStackTrace,
96101
ignoreStackEntries: options.fullStack ? [] : undefined,

packages/vitest/src/node/reporters/base.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,6 @@ export abstract class BaseReporter implements Reporter {
965965
project: this.ctx.getProjectByName(tasks[0].file.projectName || ''),
966966
verbose: this.verbose,
967967
screenshotPaths,
968-
task: tasks[0],
969968
})
970969

971970
if (tasks[0].type === 'test' && tasks[0].annotations.length) {

packages/vitest/src/node/reporters/github-actions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { File, TestAnnotation } from '@vitest/runner'
1+
import type { TestAnnotation } from '@vitest/runner'
22
import type { SerializedError } from '@vitest/utils'
33
import type { Vitest } from '../core'
44
import type { TestProject } from '../project'
@@ -9,7 +9,6 @@ import { stripVTControlCharacters } from 'node:util'
99
import { getFullName, getTasks } from '@vitest/runner/utils'
1010
import { deepMerge } from '@vitest/utils/helpers'
1111
import { relative } from 'pathe'
12-
import { capturePrintError } from '../printError'
1312
import { noun } from './renderers/utils'
1413

1514
export interface GithubActionsReporterOptions {
@@ -127,7 +126,6 @@ export class GithubActionsReporter implements Reporter {
127126
project: TestProject
128127
title: string
129128
error: unknown
130-
file?: File
131129
}>()
132130
for (const error of errors) {
133131
projectErrors.push({
@@ -150,15 +148,14 @@ export class GithubActionsReporter implements Reporter {
150148
project,
151149
title: project.name ? `[${project.name}] ${title}` : title,
152150
error,
153-
file,
154151
})
155152
}
156153
}
157154
}
158155

159156
// format errors via `printError`
160-
for (const { project, title, error, file } of projectErrors) {
161-
const result = capturePrintError(error, this.ctx, { project, task: file })
157+
for (const { project, title, error } of projectErrors) {
158+
const result = this.ctx.logger.formatError(error, { project })
162159
const stack = result?.nearest
163160
if (!stack) {
164161
continue

packages/vitest/src/node/reporters/junit.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { stripVTControlCharacters } from 'node:util'
99
import { getSuites } from '@vitest/runner/utils'
1010
import { basename, dirname, relative, resolve } from 'pathe'
1111
import { getOutputFile } from '../../utils/config-helpers'
12-
import { capturePrintError } from '../printError'
1312
import { IndentedLogger } from './renderers/indented-logger'
1413

1514
export interface ClassnameTemplateVariables {
@@ -396,11 +395,9 @@ export class JUnitReporter implements Reporter {
396395
return
397396
}
398397

399-
const result = capturePrintError(
400-
error,
401-
this.ctx,
402-
{ project: this.ctx.getProjectByName(task.file?.projectName ?? ''), task },
403-
)
398+
const result = this.ctx.logger.formatError(error, {
399+
project: this.ctx.getProjectByName(task.file?.projectName ?? ''),
400+
})
404401
await this.baseLog(
405402
escapeXML(stripVTControlCharacters(result.output.trim())),
406403
)

0 commit comments

Comments
 (0)