[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: print error properties only in verbose reporter (#5917)

authored by

Vladimir and committed by
GitHub
(Jun 19, 2024, 8:40 AM +0200) 2bd8d9d6 bc4b6073

+54 -7
+11
test/reporters/tests/default.test.ts
··· 57 57 expect(vitest.stdout).not.toContain('✓ b1 test') 58 58 expect(vitest.stdout).not.toContain('✓ b2 test') 59 59 }) 60 + 61 + test('doesn\'t print error properties', async () => { 62 + const result = await runVitest({ 63 + root: 'fixtures/error-props', 64 + reporters: 'default', 65 + env: { CI: '1' }, 66 + }) 67 + 68 + expect(result.stderr).not.toContain(`Serialized Error`) 69 + expect(result.stderr).not.toContain(`status: 'not found'`) 70 + }) 60 71 }, 120000)
+10
test/reporters/tests/verbose.test.ts
··· 14 14 ✓ basic.test.ts > slow [...]ms 15 15 `) 16 16 }) 17 + 18 + test('prints error properties', async () => { 19 + const result = await runVitest({ 20 + root: 'fixtures/error-props', 21 + reporters: 'verbose', 22 + env: { CI: '1' }, 23 + }) 24 + 25 + expect(result.stderr).toContain(`Serialized Error: { code: 404, status: 'not found' }`) 26 + })
+14 -4
packages/vitest/src/node/error.ts
··· 26 26 logger: Logger 27 27 fullStack?: boolean 28 28 showCodeFrame?: boolean 29 + printProperties?: boolean 29 30 } 30 31 31 32 interface PrintErrorResult { ··· 57 58 project: WorkspaceProject | undefined, 58 59 options: PrintErrorOptions, 59 60 ): PrintErrorResult | undefined { 60 - const { showCodeFrame = true, fullStack = false, type } = options 61 + const { showCodeFrame = true, fullStack = false, type, printProperties = true } = options 61 62 const logger = options.logger 62 63 let e = error as ErrorWithDiff 63 64 ··· 109 110 } 110 111 }) 111 112 112 - const errorProperties = getErrorProperties(e) 113 + const errorProperties = printProperties 114 + ? getErrorProperties(e) 115 + : {} 113 116 114 117 if (type) { 115 118 printErrorType(type, project.ctx) ··· 350 353 if (stack.length) { 351 354 logger.error() 352 355 } 353 - const hasProperties = Object.keys(errorProperties).length > 0 354 - if (hasProperties) { 356 + if (hasProperties(errorProperties)) { 355 357 logger.error(c.red(c.dim(divider()))) 356 358 const propertiesString = inspect(errorProperties) 357 359 logger.error(c.red(c.bold('Serialized Error:')), c.gray(propertiesString)) 358 360 } 361 + } 362 + 363 + function hasProperties(obj: any) { 364 + // eslint-disable-next-line no-unreachable-loop 365 + for (const _key in obj) { 366 + return true 367 + } 368 + return false 359 369 } 360 370 361 371 export function generateCodeFrame(
+3 -2
packages/vitest/src/node/logger.ts
··· 16 16 type?: string 17 17 fullStack?: boolean 18 18 project?: WorkspaceProject 19 + verbose?: boolean 19 20 } 20 21 21 22 const ESC = '\x1B[' ··· 89 90 90 91 printError(err: unknown, options: ErrorOptions = {}) { 91 92 const { fullStack = false, type } = options 92 - const project 93 - = options.project 93 + const project = options.project 94 94 ?? this.ctx.getCoreWorkspaceProject() 95 95 ?? this.ctx.projects[0] 96 96 printError(err, project, { ··· 98 98 type, 99 99 showCodeFrame: true, 100 100 logger: this, 101 + printProperties: options.verbose, 101 102 }) 102 103 } 103 104
+11
test/reporters/fixtures/error-props/basic.test.ts
··· 1 + import { test } from 'vitest'; 2 + 3 + test('throws', () => { 4 + const error = new Error('error with properties') 5 + Object.assign(error, { 6 + code: 404, 7 + status: 'not found', 8 + }) 9 + throw error 10 + }); 11 +
+3 -1
packages/vitest/src/node/reporters/base.ts
··· 65 65 isTTY: boolean 66 66 ctx: Vitest = undefined! 67 67 68 + protected verbose = false 69 + 68 70 private _filesInWatchMode = new Map<string, number>() 69 71 private _lastRunTimeout = 0 70 72 private _lastRunTimer: NodeJS.Timer | undefined ··· 601 603 ) 602 604 } 603 605 const project = this.ctx.getProjectByTaskId(tasks[0].id) 604 - this.ctx.logger.printError(error, { project }) 606 + this.ctx.logger.printError(error, { project, verbose: this.verbose }) 605 607 errorDivider() 606 608 } 607 609 }
+2
packages/vitest/src/node/reporters/verbose.ts
··· 6 6 import { formatProjectName, getStateSymbol } from './renderers/utils' 7 7 8 8 export class VerboseReporter extends DefaultReporter { 9 + protected verbose = true 10 + 9 11 constructor() { 10 12 super() 11 13 this.rendererOptions.renderSucceed = true