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 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
13 changes: 10 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 All @@ -10,6 +10,12 @@ import { F_POINTER } from '../utils/figures'
import type { Vitest } from './core'
import { unifiedDiff } from './diff'

export function fileFromParsedStack(stack: ParsedStack) {
if (stack?.sourcePos?.source?.startsWith('..'))
return join(stack.file, '../', stack.sourcePos.source)
return stack.file
}

export async function printError(error: unknown, ctx: Vitest) {
let e = error as ErrorWithDiff

Expand All @@ -32,7 +38,7 @@ 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 sourceCode = await fs.readFile(fileFromParsedStack(nearest), 'utf-8')
ctx.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)))
}
})
Expand Down Expand Up @@ -100,7 +106,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 = fileFromParsedStack(frame)
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