Skip to content

Commit

Permalink
feat: make error output nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Dalecky authored and Vadim Dalecky committed Nov 8, 2017
1 parent 8d458b0 commit 6036814
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
]
},
"dependencies": {
"chalk": "^2.3.0"
"chalk": "^2.3.0",
"strip-ansi": "4.0.0"
}
}
24 changes: 19 additions & 5 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ class LineWriter {

formatFailureMessage (message) {
const [firstLine, ...lines] = message.split('\n');
const formattedLines = [];
const outputLines = [];
const whitespace = ' ';

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

let firstLineFormatted = firstLine;
Expand All @@ -115,9 +117,22 @@ class LineWriter {
push('');

let internalsStarted = false;
let isFirstTraceLine = true;

for (const line of lines) {
if (line.match(REG_AT)) {
if (isFirstTraceLine) {
isFirstTraceLine = false;

const isLastLineBlank = outputLines[outputLines.length - 1] === '';

if (!isLastLineBlank) {
push('');
}
push(chalk`{bold.dim Stack trace:}`);
push('');
}

const matches = line.match(REG_TRACE_LINE);

if (matches) {
Expand All @@ -139,13 +154,12 @@ class LineWriter {
}
} else {
push(line);
push('');
}
}

push('');

return formattedLines.join('\n');
return outputLines.map((line) => formatComment(whitespace + line)).join('\n');
}

errors (messages) {
Expand Down
12 changes: 3 additions & 9 deletions test/TapReporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,8 @@ describe('TapReporter', () => {
const options = {};
const tapReporter = new TapReporter(globalConfig, options);

expect(tapReporter._globalConfig).toBe(globalConfig);
expect(tapReporter._options).toBe(options);
});

test('must set _shouldFail to false by default', () => {
const tapReporter = new TapReporter();

expect(tapReporter._shouldFail).toBe(false);
expect(tapReporter.globalConfig).toBe(globalConfig);
expect(tapReporter.options).toBe(options);
});

test('must log the start of the tests', () => {
Expand Down Expand Up @@ -128,7 +122,7 @@ describe('TapReporter', () => {
expect(tapReporter.writer.pending.mock.calls).toMatchSnapshot();
});

test.only('TapReporter onTestResults must output all the tests on a suite tests', () => {
test('must output all the tests on a suite tests', () => {
const tapReporter = new TapReporter();

tapReporter.onTestResult({}, severalTestsSuite);
Expand Down

0 comments on commit 6036814

Please sign in to comment.