Skip to content

Commit

Permalink
feat: format nicely stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Dalecky authored and Vadim Dalecky committed Nov 7, 2017
1 parent 2c28898 commit 02086cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
66 changes: 43 additions & 23 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ const path = require('path');
const chalk = require('chalk');

const REG_TRACE_LINE = /\s*(.+)\((.+):([0-9]+):([0-9]+)\)$/;
const REG_INTERNALS = /^(node_modules|internal)\//;
const REG_AT = /^\s*at/;
const REG_ERROR = /^\s*Error:\s*/;

const MDASH = '\u2014';
const CIRCLE = '●';

const formatComment = (line) => chalk`{hidden #} ${line}`;
const formatFailureMessageTraceLine = (description, relativeFilePath, row, column) =>
chalk`${description}({cyan ${relativeFilePath}}:{black.bold ${row}}:{black.bold ${column}})`;

class LineWriter {
constructor (logger, root) {
this.counter = 0;
Expand All @@ -23,7 +31,7 @@ class LineWriter {
}

comment (line) {
this.logger.info(chalk`{hidden #} ${line}`);
this.logger.info(formatComment(line));
}

commentLight (line) {
Expand Down Expand Up @@ -75,39 +83,51 @@ class LineWriter {
return path.relative(this.root, filePath);
}

formatFailureMessageTraceLine (line) {
const matches = line.match(REG_TRACE_LINE);

if (matches) {
const [, description, file, row, column] = matches;

return chalk`${description}({cyan ${this.getPathRelativeToRoot(file)}}:{black.bold ${row}}:{black.bold ${column}})`;
} else {
return line;
}
}

formatFailureMessage (message) {
const [firstLine, ...lines] = message.split('\n');
const formattedLines = [];
const whitespace = ' '.repeat(9 + String(this.counter).length);
const whitespace = ' ';

const push = (line) => {
const formattedLine = chalk`{hidden #}${whitespace}${line}`;
const push = (line) => formattedLines.push(formatComment(whitespace + line));
const pushTraceLine = (line) => push(chalk` {grey ${line}}`);
const pushTraceLineDim = (line) => pushTraceLine(chalk`{dim ${line}}`);

formattedLines.push(formattedLine);
};
let firstLineFormatted = firstLine;

const pushTraceLine = (line) => {
push(chalk` {grey ${line}}`);
};
// Remove leading `Error: `
firstLineFormatted = firstLineFormatted.replace(REG_ERROR, '');

push('');
push(firstLine);
push(firstLineFormatted);
push('');

let internalsStarted = false;

for (const line of lines) {
pushTraceLine(this.formatFailureMessageTraceLine(line));
if (line.match(REG_AT)) {
const matches = line.match(REG_TRACE_LINE);

if (matches) {
const [, description, file, row, column] = matches;
const relativeFilePath = path.relative(this.root, file);

if (relativeFilePath.match(REG_INTERNALS)) {
internalsStarted = true;
}

// eslint-disable-next-line no-lonely-if
if (internalsStarted) {
pushTraceLineDim(formatFailureMessageTraceLine(description, relativeFilePath, row, column));
} else {
pushTraceLine(formatFailureMessageTraceLine(description, relativeFilePath, row, column));
}
} else {
pushTraceLine(line);
}
} else {
push(line);
push('');
}
}

push('');
Expand Down
3 changes: 1 addition & 2 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class TapReporter {
}

onRunComplete (contexts, results) {
const {estimatedTime} = this.onRunStartOptions;
const {
numFailedTestSuites,
numFailedTests,
Expand All @@ -94,8 +95,6 @@ class TapReporter {
startTime
} = results;

const {estimatedTime} = this.onRunStartOptions;

this._shouldFail = numFailedTestSuites > 0 || numFailedTests > 0;

this.writer.blank();
Expand Down
11 changes: 1 addition & 10 deletions test/TapReporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ const {
skippedTestSuite
} = require('./fixtures');

jest.mock('chalk', () => ({
bgBlue: (str) => str,
bgGreen: (str) => str,
bgRed: (str) => str,
black: (str) => str,
green: (str) => str,
grey: (str) => str,
red: (str) => str,
yellow: (str) => str
}));
jest.mock('chalk', () => jest.fn());

let origLog;

Expand Down

0 comments on commit 02086cc

Please sign in to comment.