Skip to content

Commit

Permalink
fix: print error properties only in verbose reporter (#5917)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 19, 2024
1 parent bc4b607 commit 2bd8d9d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
18 changes: 14 additions & 4 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface PrintErrorOptions {
logger: Logger
fullStack?: boolean
showCodeFrame?: boolean
printProperties?: boolean
}

interface PrintErrorResult {
Expand Down Expand Up @@ -57,7 +58,7 @@ export function printError(
project: WorkspaceProject | undefined,
options: PrintErrorOptions,
): PrintErrorResult | undefined {
const { showCodeFrame = true, fullStack = false, type } = options
const { showCodeFrame = true, fullStack = false, type, printProperties = true } = options
const logger = options.logger
let e = error as ErrorWithDiff

Expand Down Expand Up @@ -109,7 +110,9 @@ export function printError(
}
})

const errorProperties = getErrorProperties(e)
const errorProperties = printProperties
? getErrorProperties(e)
: {}

if (type) {
printErrorType(type, project.ctx)
Expand Down Expand Up @@ -350,14 +353,21 @@ export function printStack(
if (stack.length) {
logger.error()
}
const hasProperties = Object.keys(errorProperties).length > 0
if (hasProperties) {
if (hasProperties(errorProperties)) {
logger.error(c.red(c.dim(divider())))
const propertiesString = inspect(errorProperties)
logger.error(c.red(c.bold('Serialized Error:')), c.gray(propertiesString))
}
}

function hasProperties(obj: any) {
// eslint-disable-next-line no-unreachable-loop
for (const _key in obj) {
return true
}
return false
}

export function generateCodeFrame(
source: string,
indent = 0,
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ErrorOptions {
type?: string
fullStack?: boolean
project?: WorkspaceProject
verbose?: boolean
}

const ESC = '\x1B['
Expand Down Expand Up @@ -89,15 +90,15 @@ export class Logger {

printError(err: unknown, options: ErrorOptions = {}) {
const { fullStack = false, type } = options
const project
= options.project
const project = options.project
?? this.ctx.getCoreWorkspaceProject()
?? this.ctx.projects[0]
printError(err, project, {
fullStack,
type,
showCodeFrame: true,
logger: this,
printProperties: options.verbose,
})
}

Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export abstract class BaseReporter implements Reporter {
isTTY: boolean
ctx: Vitest = undefined!

protected verbose = false

private _filesInWatchMode = new Map<string, number>()
private _lastRunTimeout = 0
private _lastRunTimer: NodeJS.Timer | undefined
Expand Down Expand Up @@ -601,7 +603,7 @@ export abstract class BaseReporter implements Reporter {
)
}
const project = this.ctx.getProjectByTaskId(tasks[0].id)
this.ctx.logger.printError(error, { project })
this.ctx.logger.printError(error, { project, verbose: this.verbose })
errorDivider()
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/reporters/verbose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { DefaultReporter } from './default'
import { formatProjectName, getStateSymbol } from './renderers/utils'

export class VerboseReporter extends DefaultReporter {
protected verbose = true

constructor() {
super()
this.rendererOptions.renderSucceed = true
Expand Down
11 changes: 11 additions & 0 deletions test/reporters/fixtures/error-props/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from 'vitest';

test('throws', () => {
const error = new Error('error with properties')
Object.assign(error, {
code: 404,
status: 'not found',
})
throw error
});

11 changes: 11 additions & 0 deletions test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ describe('default reporter', async () => {
expect(vitest.stdout).not.toContain('✓ b1 test')
expect(vitest.stdout).not.toContain('✓ b2 test')
})

test('doesn\'t print error properties', async () => {
const result = await runVitest({
root: 'fixtures/error-props',
reporters: 'default',
env: { CI: '1' },
})

expect(result.stderr).not.toContain(`Serialized Error`)
expect(result.stderr).not.toContain(`status: 'not found'`)
})
}, 120000)
10 changes: 10 additions & 0 deletions test/reporters/tests/verbose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ test('duration', async () => {
✓ basic.test.ts > slow [...]ms
`)
})

test('prints error properties', async () => {
const result = await runVitest({
root: 'fixtures/error-props',
reporters: 'verbose',
env: { CI: '1' },
})

expect(result.stderr).toContain(`Serialized Error: { code: 404, status: 'not found' }`)
})

0 comments on commit 2bd8d9d

Please sign in to comment.