|
10 | 10 | import type {Glob, Path} from 'types/Config'; |
11 | 11 | import type {AssertionResult, TestResult} from 'types/TestResult'; |
12 | 12 |
|
| 13 | +import fs from 'fs'; |
13 | 14 | import path from 'path'; |
14 | 15 | import chalk from 'chalk'; |
15 | 16 | import micromatch from 'micromatch'; |
16 | 17 | import slash from 'slash'; |
| 18 | +import {codeFrameColumns} from '@babel/code-frame'; |
17 | 19 | import StackUtils from 'stack-utils'; |
18 | 20 |
|
19 | 21 | let nodeInternals = []; |
@@ -194,15 +196,45 @@ export const formatStackTrace = ( |
194 | 196 | testPath: ?Path, |
195 | 197 | ) => { |
196 | 198 | let lines = stack.split(/\n/); |
| 199 | + let renderedCallsite = ''; |
197 | 200 | const relativeTestPath = testPath |
198 | 201 | ? slash(path.relative(config.rootDir, testPath)) |
199 | 202 | : null; |
200 | 203 | 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 |
202 | 232 | .map(trimPaths) |
203 | 233 | .map(formatPaths.bind(null, config, options, relativeTestPath)) |
204 | 234 | .map(line => STACK_INDENT + line) |
205 | 235 | .join('\n'); |
| 236 | + |
| 237 | + return renderedCallsite + stacktrace; |
206 | 238 | }; |
207 | 239 |
|
208 | 240 | export const formatResultsErrors = ( |
|
0 commit comments