Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): improve colors used when erroring #3349

Merged
merged 5 commits into from May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/vitest/src/node/error.ts
Expand Up @@ -57,6 +57,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
printErrorType(type, ctx)
printErrorMessage(e, ctx.logger)

// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
if (e.diff)
displayDiff(e.diff, ctx.logger.console)

// if the error provide the frame
if (e.frame) {
ctx.logger.error(c.yellow(e.frame))
Expand All @@ -65,7 +69,7 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
printStack(ctx, stacks, nearest, errorProperties, (s) => {
if (showCodeFrame && s === nearest && nearest) {
const sourceCode = readFileSync(nearest.file, 'utf-8')
ctx.logger.error(c.yellow(generateCodeFrame(sourceCode, 4, s.line, s.column)))
ctx.logger.error(generateCodeFrame(sourceCode, 4, s.line, s.column))
}
})
}
Expand Down Expand Up @@ -93,10 +97,6 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
}

handleImportOutsideModuleError(e.stack || e.stackStr || '', ctx)

// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
if (e.diff)
displayDiff(e.diff, ctx.logger.console)
}

function printErrorType(type: string, ctx: Vitest) {
Expand Down Expand Up @@ -185,7 +185,7 @@ function printModuleWarningForSourceCode(logger: Logger, path: string) {
}

export function displayDiff(diff: string, console: Console) {
console.error(diff)
console.error(`\n${diff}\n`)
}

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

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

logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${frame.line}:${frame.column}`)].filter(Boolean).join(' ')}`))
logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, `${path}:${c.dim(`${frame.line}:${frame.column}`)}`].filter(Boolean).join(' ')}`))
onStack?.(frame)
}
if (stack.length)
Expand Down
18 changes: 12 additions & 6 deletions test/core/test/diff.test.ts
Expand Up @@ -10,14 +10,16 @@ test('displays object diff', () => {
setupColors(getDefaultColors())
displayDiff(unifiedDiff(objectA, objectB), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
" - Expected - 1
"
- Expected - 1
+ Received + 1

{
a: 1,
- b: 3,
+ b: 2,
}"
}
"
`)
})

Expand All @@ -28,11 +30,13 @@ test('display one line string diff', () => {
setupColors(getDefaultColors())
displayDiff(unifiedDiff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
" - Expected - 1
"
- Expected - 1
+ Received + 1

- 'string2'
+ 'string1'"
+ 'string1'
"
`)
})

Expand All @@ -43,13 +47,15 @@ test('display multiline line string diff', () => {
setupColors(getDefaultColors())
displayDiff(unifiedDiff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
" - Expected - 2
"
- Expected - 2
+ Received + 2

+ string1
\`string2
- string2
- string1\`
+ string3\`"
+ string3\`
"
`)
})