Skip to content

Commit

Permalink
assert: make skipping indicator blue
Browse files Browse the repository at this point in the history
If lines gets skipped, they are marked with three dots. This makes
sure they are better visualized to distinguish them from everything
else.

PR-URL: nodejs#20315
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
  • Loading branch information
BridgeAR committed Apr 29, 2018
1 parent f0a6cb0 commit 8a0fb13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 12 additions & 8 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const kInfo = Symbol('info');
const messages = new Map();
const codes = {};

let blue = '';
let green = '';
let red = '';
let white = '';
Expand Down Expand Up @@ -259,7 +260,7 @@ function createErrDiff(actual, expected, operator) {
const expectedLines = inspectValue(expected);
const msg = READABLE_OPERATOR[operator] +
`:\n${green}+ expected${white} ${red}- actual${white}`;
const skippedMsg = ' ... Lines skipped';
const skippedMsg = ` ${blue}...${white} Lines skipped`;

// Remove all ending lines that match (this optimizes the output for
// readability by reducing the number of total changed lines).
Expand All @@ -280,7 +281,7 @@ function createErrDiff(actual, expected, operator) {
b = expectedLines[expectedLines.length - 1];
}
if (i > 3) {
end = `\n...${end}`;
end = `\n${blue}...${white}${end}`;
skipped = true;
}
if (other !== '') {
Expand All @@ -297,7 +298,7 @@ function createErrDiff(actual, expected, operator) {
if (actualLines.length < i + 1) {
if (cur > 1 && i > 2) {
if (cur > 4) {
res += '\n...';
res += `\n${blue}...${white}`;
skipped = true;
} else if (cur > 3) {
res += `\n ${expectedLines[i - 2]}`;
Expand All @@ -313,7 +314,7 @@ function createErrDiff(actual, expected, operator) {
} else if (expectedLines.length < i + 1) {
if (cur > 1 && i > 2) {
if (cur > 4) {
res += '\n...';
res += `\n${blue}...${white}`;
skipped = true;
} else if (cur > 3) {
res += `\n ${actualLines[i - 2]}`;
Expand All @@ -329,7 +330,7 @@ function createErrDiff(actual, expected, operator) {
} else if (actualLines[i] !== expectedLines[i]) {
if (cur > 1 && i > 2) {
if (cur > 4) {
res += '\n...';
res += `\n${blue}...${white}`;
skipped = true;
} else if (cur > 3) {
res += `\n ${actualLines[i - 2]}`;
Expand All @@ -354,7 +355,8 @@ function createErrDiff(actual, expected, operator) {
}
// Inspected object to big (Show ~20 rows max)
if (printedLines > 20 && i < maxLines - 2) {
return `${msg}${skippedMsg}\n${res}\n...${other}\n...`;
return `${msg}${skippedMsg}\n${res}\n${blue}...${white}${other}\n` +
`${blue}...${white}`;
}
}

Expand All @@ -371,7 +373,7 @@ function createErrDiff(actual, expected, operator) {
// Only remove lines in case it makes sense to collapse those.
// TODO: Accept env to always show the full error.
if (actualLines.length > 30) {
actualLines[26] = '...';
actualLines[26] = `${blue}...${white}`;
while (actualLines.length > 27) {
actualLines.pop();
}
Expand Down Expand Up @@ -402,10 +404,12 @@ class AssertionError extends Error {
// Reset on each call to make sure we handle dynamically set environment
// variables correct.
if (process.stdout.getColorDepth() !== 1) {
blue = '\u001b[34m';
green = '\u001b[32m';
white = '\u001b[39m';
red = '\u001b[31m';
} else {
blue = '';
green = '';
white = '';
red = '';
Expand Down Expand Up @@ -435,7 +439,7 @@ class AssertionError extends Error {
// Only remove lines in case it makes sense to collapse those.
// TODO: Accept env to always show the full error.
if (res.length > 30) {
res[26] = '...';
res[26] = `${blue}...${white}`;
while (res.length > 27) {
res.pop();
}
Expand Down
7 changes: 5 additions & 2 deletions test/pseudo-tty/test-assert-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const assert = require('assert').strict;
try {
// Activate colors even if the tty does not support colors.
process.env.COLORTERM = '1';
assert.deepStrictEqual([1, 2], [2, 2]);
assert.deepStrictEqual([1, 2, 2, 2], [2, 2, 2, 2]);
} catch (err) {
const expected = 'Input A expected to strictly deep-equal input B:\n' +
'\u001b[32m+ expected\u001b[39m \u001b[31m- actual\u001b[39m\n\n' +
'\u001b[32m+ expected\u001b[39m \u001b[31m- actual\u001b[39m' +
' \u001b[34m...\u001b[39m Lines skipped\n\n' +
' [\n' +
'\u001b[31m-\u001b[39m 1,\n' +
'\u001b[32m+\u001b[39m 2,\n' +
' 2,\n' +
'\u001b[34m...\u001b[39m\n' +
' 2\n' +
' ]';
assert.strictEqual(err.message, expected);
Expand Down

0 comments on commit 8a0fb13

Please sign in to comment.