Skip to content

Commit 1668179

Browse files
ghiscodingsheremet-va
andauthoredMay 24, 2023
fix(cli): improve colors used when erroring (#3349)
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
1 parent a2cc2b3 commit 1668179

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed
 

‎packages/vitest/src/node/error.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
5757
printErrorType(type, ctx)
5858
printErrorMessage(e, ctx.logger)
5959

60+
// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
61+
if (e.diff)
62+
displayDiff(e.diff, ctx.logger.console)
63+
6064
// if the error provide the frame
6165
if (e.frame) {
6266
ctx.logger.error(c.yellow(e.frame))
@@ -65,7 +69,7 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
6569
printStack(ctx, stacks, nearest, errorProperties, (s) => {
6670
if (showCodeFrame && s === nearest && nearest) {
6771
const sourceCode = readFileSync(nearest.file, 'utf-8')
68-
ctx.logger.error(c.yellow(generateCodeFrame(sourceCode, 4, s.line, s.column)))
72+
ctx.logger.error(generateCodeFrame(sourceCode, 4, s.line, s.column))
6973
}
7074
})
7175
}
@@ -93,10 +97,6 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
9397
}
9498

9599
handleImportOutsideModuleError(e.stack || e.stackStr || '', ctx)
96-
97-
// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
98-
if (e.diff)
99-
displayDiff(e.diff, ctx.logger.console)
100100
}
101101

102102
function printErrorType(type: string, ctx: Vitest) {
@@ -185,7 +185,7 @@ function printModuleWarningForSourceCode(logger: Logger, path: string) {
185185
}
186186

187187
export function displayDiff(diff: string, console: Console) {
188-
console.error(diff)
188+
console.error(`\n${diff}\n`)
189189
}
190190

191191
function printErrorMessage(error: ErrorWithDiff, logger: Logger) {
@@ -203,10 +203,10 @@ function printStack(
203203
const logger = ctx.logger
204204

205205
for (const frame of stack) {
206-
const color = frame === highlight ? c.yellow : c.gray
206+
const color = frame === highlight ? c.cyan : c.gray
207207
const path = relative(ctx.config.root, frame.file)
208208

209-
logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${frame.line}:${frame.column}`)].filter(Boolean).join(' ')}`))
209+
logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, `${path}:${c.dim(`${frame.line}:${frame.column}`)}`].filter(Boolean).join(' ')}`))
210210
onStack?.(frame)
211211
}
212212
if (stack.length)

‎test/core/test/diff.test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ test('displays object diff', () => {
1010
setupColors(getDefaultColors())
1111
displayDiff(unifiedDiff(objectA, objectB), console as any)
1212
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
13-
" - Expected - 1
13+
"
14+
- Expected - 1
1415
+ Received + 1
1516
1617
{
1718
a: 1,
1819
- b: 3,
1920
+ b: 2,
20-
}"
21+
}
22+
"
2123
`)
2224
})
2325

@@ -28,11 +30,13 @@ test('display one line string diff', () => {
2830
setupColors(getDefaultColors())
2931
displayDiff(unifiedDiff(string1, string2), console as any)
3032
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
31-
" - Expected - 1
33+
"
34+
- Expected - 1
3235
+ Received + 1
3336
3437
- 'string2'
35-
+ 'string1'"
38+
+ 'string1'
39+
"
3640
`)
3741
})
3842

@@ -43,13 +47,15 @@ test('display multiline line string diff', () => {
4347
setupColors(getDefaultColors())
4448
displayDiff(unifiedDiff(string1, string2), console as any)
4549
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
46-
" - Expected - 2
50+
"
51+
- Expected - 2
4752
+ Received + 2
4853
4954
+ string1
5055
\`string2
5156
- string2
5257
- string1\`
53-
+ string3\`"
58+
+ string3\`
59+
"
5460
`)
5561
})

0 commit comments

Comments
 (0)
Please sign in to comment.