Skip to content

Commit 10e9de9

Browse files
committed
feat(jest-message-util): render codeframe on failure
1 parent cfb7ee1 commit 10e9de9

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

packages/jest-message-util/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"license": "MIT",
99
"main": "build/index.js",
1010
"dependencies": {
11+
"@babel/code-frame": "^7.0.0-beta.35",
1112
"chalk": "^2.0.1",
1213
"micromatch": "^2.3.11",
1314
"slash": "^1.0.0",

packages/jest-message-util/src/index.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import type {Glob, Path} from 'types/Config';
1111
import type {AssertionResult, TestResult} from 'types/TestResult';
1212

13+
import fs from 'fs';
1314
import path from 'path';
1415
import chalk from 'chalk';
1516
import micromatch from 'micromatch';
1617
import slash from 'slash';
18+
import {codeFrameColumns} from '@babel/code-frame';
1719
import StackUtils from 'stack-utils';
1820

1921
let 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

208240
export 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
};

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
# yarn lockfile v1
33

44

5+
"@babel/code-frame@^7.0.0-beta.35":
6+
version "7.0.0-beta.35"
7+
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.35.tgz#04eeb6dca7efef8f65776a4c214157303b85ad51"
8+
dependencies:
9+
chalk "^2.0.0"
10+
esutils "^2.0.2"
11+
js-tokens "^3.0.0"
12+
513
"@types/node@*":
614
version "8.0.53"
715
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"

0 commit comments

Comments
 (0)