Skip to content

Commit

Permalink
fix: render original source if mapped in node (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Mar 20, 2022
1 parent 5eac4ca commit 42406a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/vitest/src/node/error.ts
@@ -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
Expand Up @@ -47,6 +47,7 @@ export interface UserConsoleLog {
}

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

0 comments on commit 42406a3

Please sign in to comment.