Skip to content

Commit c45097e

Browse files
authored
refactor: use bg* colors in reporter instead of reversing (#7785)
1 parent ce2a06b commit c45097e

6 files changed

Lines changed: 27 additions & 19 deletions

File tree

packages/vitest/src/node/cli/cac.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ async function start(mode: VitestRunMode, cliFilters: string[], options: CliOpti
308308
}
309309
}
310310
catch (e) {
311-
const { divider } = await import('../reporters/renderers/utils')
312-
console.error(`\n${c.red(divider(c.bold(c.inverse(' Startup Error '))))}`)
311+
const { errorBanner } = await import('../reporters/renderers/utils')
312+
console.error(`\n${errorBanner('Startup Error')}`)
313313
console.error(e)
314314
console.error('\n\n')
315315

@@ -365,8 +365,8 @@ async function collect(mode: VitestRunMode, cliFilters: string[], options: CliOp
365365
await ctx.close()
366366
}
367367
catch (e) {
368-
const { divider } = await import('../reporters/renderers/utils')
369-
console.error(`\n${c.red(divider(c.bold(c.inverse(' Collect Error '))))}`)
368+
const { errorBanner } = await import('../reporters/renderers/utils')
369+
console.error(`\n${errorBanner('Collect Error')}`)
370370
console.error(e)
371371
console.error('\n\n')
372372

packages/vitest/src/node/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
positionToOffset,
1818
} from '../utils/source-map'
1919
import { F_POINTER } from './reporters/renderers/figures'
20-
import { divider, truncateString } from './reporters/renderers/utils'
20+
import { divider, errorBanner, truncateString } from './reporters/renderers/utils'
2121

2222
type ErrorLogger = Pick<Logger, 'error' | 'highlight'>
2323

@@ -246,7 +246,7 @@ function printErrorInner(
246246
}
247247

248248
function printErrorType(type: string, ctx: Vitest) {
249-
ctx.logger.error(`\n${c.red(divider(c.bold(c.inverse(` ${type} `))))}`)
249+
ctx.logger.error(`\n${errorBanner(type)}`)
250250
}
251251

252252
const skipErrorProperties = new Set([

packages/vitest/src/node/logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { toArray } from '@vitest/utils'
99
import c from 'tinyrainbow'
1010
import { highlightCode } from '../utils/colors'
1111
import { printError } from './error'
12-
import { divider, formatProjectName, withLabel } from './reporters/renderers/utils'
12+
import { divider, errorBanner, formatProjectName, withLabel } from './reporters/renderers/utils'
1313
import { RandomSequencer } from './sequencers/RandomSequencer'
1414

1515
export interface ErrorOptions {
@@ -262,7 +262,7 @@ export class Logger {
262262
+ '\nThis might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.',
263263
),
264264
)
265-
this.error(c.red(divider(c.bold(c.inverse(' Unhandled Errors ')))))
265+
this.error(errorBanner('Unhandled Errors'))
266266
this.error(errorMessage)
267267
errors.forEach((err) => {
268268
this.printError(err, {
@@ -281,7 +281,7 @@ export class Logger {
281281
} not related to your test files.`,
282282
),
283283
)
284-
this.log(c.red(divider(c.bold(c.inverse(' Source Errors ')))))
284+
this.log(errorBanner('Source Errors'))
285285
this.log(errorMessage)
286286
errors.forEach((err) => {
287287
this.printError(err, { fullStack: true })

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import c from 'tinyrainbow'
1212
import { isTTY } from '../../utils/env'
1313
import { hasFailedSnapshot } from '../../utils/tasks'
1414
import { F_CHECK, F_POINTER, F_RIGHT } from './renderers/figures'
15-
import { countTestErrors, divider, formatProjectName, formatTime, formatTimeString, getStateString, getStateSymbol, padSummaryTitle, renderSnapshotSummary, taskFail, withLabel } from './renderers/utils'
15+
import { countTestErrors, divider, errorBanner, formatProjectName, formatTime, formatTimeString, getStateString, getStateSymbol, padSummaryTitle, renderSnapshotSummary, taskFail, withLabel } from './renderers/utils'
1616

1717
const BADGE_PADDING = ' '
1818

@@ -609,7 +609,7 @@ export abstract class BaseReporter implements Reporter {
609609
}
610610

611611
this.ctx.logger.error(
612-
`${c.red(c.bold(c.inverse(' FAIL ')))} ${formatProjectName(projectName)}${name}`,
612+
`${c.bgRed(c.bold(' FAIL '))} ${formatProjectName(projectName)}${name}`,
613613
)
614614
}
615615

@@ -627,10 +627,6 @@ export abstract class BaseReporter implements Reporter {
627627
}
628628
}
629629

630-
function errorBanner(message: string) {
631-
return c.red(divider(c.bold(c.inverse(` ${message} `))))
632-
}
633-
634630
function sum<T>(items: T[], cb: (_next: T) => number | undefined) {
635631
return items.reduce((total, next) => {
636632
return total + Math.max(cb(next) || 0, 0)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BasicReporter extends BaseReporter {
1212
onInit(ctx: Vitest): void {
1313
super.onInit(ctx)
1414

15-
ctx.logger.log(c.inverse(c.bold(c.yellow(' DEPRECATED '))), c.yellow(
15+
ctx.logger.log(c.bold(c.bgYellow(' DEPRECATED ')), c.yellow(
1616
`'basic' reporter is deprecated and will be removed in Vitest v3.\n`
1717
+ `Remove 'basic' from 'reporters' option. To match 'basic' reporter 100%, use configuration:\n${
1818
JSON.stringify({ test: { reporters: [['default', { summary: false }]] } }, null, 2)}`,

packages/vitest/src/node/reporters/renderers/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Task } from '@vitest/runner'
22
import type { SnapshotSummary } from '@vitest/snapshot'
3+
import type { Formatter } from 'tinyrainbow'
34
import { stripVTControlCharacters } from 'node:util'
45
import { slash } from '@vitest/utils'
56
import { basename, dirname, isAbsolute, relative } from 'pathe'
@@ -30,8 +31,18 @@ function getCols(delta = 0) {
3031
return Math.max(length + delta, 0)
3132
}
3233

33-
export function divider(text?: string, left?: number, right?: number): string {
34+
export function errorBanner(message: string): string {
35+
return divider(c.bold(c.bgRed(` ${message} `)), null, null, c.red)
36+
}
37+
38+
export function divider(
39+
text?: string,
40+
left?: number | null,
41+
right?: number | null,
42+
color?: Formatter,
43+
): string {
3444
const cols = getCols()
45+
const c = color || ((text: string) => text)
3546

3647
if (text) {
3748
const textLength = stripVTControlCharacters(text).length
@@ -44,7 +55,7 @@ export function divider(text?: string, left?: number, right?: number): string {
4455
}
4556
left = Math.max(0, left)
4657
right = Math.max(0, right)
47-
return `${F_LONG_DASH.repeat(left)}${text}${F_LONG_DASH.repeat(right)}`
58+
return `${c(F_LONG_DASH.repeat(left))}${text}${c(F_LONG_DASH.repeat(right))}`
4859
}
4960
return F_LONG_DASH.repeat(cols)
5061
}
@@ -229,7 +240,8 @@ export function formatProjectName(name: string | undefined, suffix = ' '): strin
229240
}
230241

231242
export function withLabel(color: 'red' | 'green' | 'blue' | 'cyan' | 'yellow', label: string, message?: string) {
232-
return `${c.bold(c.inverse(c[color](` ${label} `)))} ${message ? c[color](message) : ''}`
243+
const bgColor = `bg${color.charAt(0).toUpperCase()}${color.slice(1)}` as `bg${Capitalize<typeof color>}`
244+
return `${c.bold(c[bgColor](` ${label} `))} ${message ? c[color](message) : ''}`
233245
}
234246

235247
export function padSummaryTitle(str: string): string {

0 commit comments

Comments
 (0)