1010import type { Glob , Path } from 'types/Config' ;
1111import type { AssertionResult , TestResult } from 'types/TestResult' ;
1212
13+ import fs from 'fs' ;
1314import path from 'path' ;
1415import chalk from 'chalk' ;
1516import micromatch from 'micromatch' ;
1617import slash from 'slash' ;
18+ import { codeFrameColumns } from '@babel/code-frame' ;
1719import StackUtils from 'stack-utils' ;
1820
1921let nodeInternals = [ ] ;
@@ -194,15 +196,45 @@ export const formatStackTrace = (
194196 testPath : ?Path ,
195197) => {
196198 let lines = stack . split ( / \n / ) ;
199+ let renderedCallsite = '' ;
197200 const relativeTestPath = testPath
198201 ? slash ( path . relative ( config . rootDir , testPath ) )
199202 : null ;
200203 lines = removeInternalStackEntries ( lines , options ) ;
201- return lines
204+
205+ if ( testPath ) {
206+ const topFrame = lines
207+ . join ( '\n' )
208+ . trim ( )
209+ . split ( '\n' ) [ 0 ] ;
210+
211+ const parsedFrame = StackUtils . parseLine ( topFrame ) ;
212+
213+ if ( parsedFrame ) {
214+ renderedCallsite = codeFrameColumns (
215+ fs . readFileSync ( testPath , 'utf8' ) ,
216+ {
217+ start : { line : parsedFrame . line } ,
218+ } ,
219+ { highlightCode : true } ,
220+ ) ;
221+
222+ renderedCallsite = renderedCallsite
223+ . split ( '\n' )
224+ . map ( line => MESSAGE_INDENT + line )
225+ . join ( '\n' ) ;
226+
227+ renderedCallsite = `\n\n${ renderedCallsite } \n\n` ;
228+ }
229+ }
230+
231+ const stacktrace = lines
202232 . map ( trimPaths )
203233 . map ( formatPaths . bind ( null , config , options , relativeTestPath ) )
204234 . map ( line => STACK_INDENT + line )
205235 . join ( '\n' ) ;
236+
237+ return renderedCallsite + stacktrace ;
206238} ;
207239
208240export const formatResultsErrors = (
@@ -222,14 +254,14 @@ export const formatResultsErrors = (
222254
223255 return failedResults
224256 . map ( ( { result, content} ) => {
225- let { message , stack } = separateMessageFromStack ( content ) ;
226- stack = options . noStackTrace
257+ const separated = separateMessageFromStack ( content ) ;
258+ const formattedStack = options . noStackTrace
227259 ? ''
228260 : STACK_TRACE_COLOR (
229- formatStackTrace ( stack , config , options , testPath ) ,
261+ formatStackTrace ( separated . stack , config , options , testPath ) ,
230262 ) + '\n' ;
231263
232- message = message
264+ const message = separated . message
233265 . split ( / \n / )
234266 . map ( line => MESSAGE_INDENT + line )
235267 . join ( '\n' ) ;
@@ -243,7 +275,7 @@ export const formatResultsErrors = (
243275 result . title ,
244276 ) + '\n' ;
245277
246- return title + '\n' + message + '\n' + stack ;
278+ return title + '\n' + message + '\n' + formattedStack ;
247279 } )
248280 . join ( '\n' ) ;
249281} ;
0 commit comments