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: render original source if mapped in node #987

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable prefer-template */
/* eslint-disable no-template-curly-in-string */
import { existsSync, promises as fs } from 'fs'
import { relative } from 'pathe'
import { join, relative } from 'pathe'
import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import type { ErrorWithDiff, ParsedStack, Position } from '../types'
Expand Down Expand Up @@ -32,7 +32,8 @@ export async function printError(error: unknown, ctx: Vitest) {
printErrorMessage(e, ctx.console)
await printStack(ctx, stacks, nearest, async(s, pos) => {
if (s === nearest && nearest) {
const sourceCode = await fs.readFile(nearest.file, 'utf-8')
const file = nearest.sourcePos && nearest.sourcePos.source && nearest.sourcePos.source !== '.' ? join(nearest.file, '../', nearest.sourcePos.source) : nearest.file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use ?. here, and I think it will be good to extract a fileFromSourcePosition(position) utility so it can be reused in printStack

Suggested change
const file = nearest.sourcePos && nearest.sourcePos.source && nearest.sourcePos.source !== '.' ? join(nearest.file, '../', nearest.sourcePos.source) : nearest.file
const file = nearest?.sourcePos?.source !== '.' ? join(nearest.file, '../', nearest.sourcePos.source) : nearest.file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted as: fileFromParsedStack, any idea why some tests appear to be failing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The errors look unrelated, it seems an issue with a test expectation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved

const sourceCode = await fs.readFile(file, 'utf-8')
ctx.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)))
}
})
Expand Down Expand Up @@ -100,7 +101,8 @@ async function printStack(
for (const frame of stack) {
const pos = frame.sourcePos || frame
const color = frame === highlight ? c.yellow : c.gray
const path = relative(ctx.config.root, frame.file)
const file = frame.sourcePos && frame.sourcePos.source && frame.sourcePos.source !== '.' ? join(frame.file, '../', frame.sourcePos.source) : frame.file
const path = relative(ctx.config.root, file)

ctx.log(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${pos.line}:${pos.column}`)].filter(Boolean).join(' ')}`))
await onStack?.(frame, pos)
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface UserConsoleLog {
}

export interface Position {
source?: string
line: number
column: number
}
Expand Down