Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 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,33 +355,31 @@ 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}`;
}
}

// Strict equal with identical objects that are not identical by reference.
if (identical === maxLines) {
let base = 'Input object identical but not reference equal:';

if (operator !== 'strictEqual') {
// This code path should not be possible to reach.
// The output is identical but it is not clear why.
base = 'Input objects not identical:';
}
// E.g., assert.deepStrictEqual(Symbol(), Symbol())
const base = operator === 'strictEqual' ?
'Input objects identical but not reference equal:' :
'Input objects not identical:';

// We have to get the result again. The lines were all removed before.
const actualLines = inspectValue(actual);

// 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();
}
}

return `${base}\n\n ${actualLines.join('\n ')}\n`;
return `${base}\n\n${actualLines.join('\n')}\n`;
}
return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}`;
}
Expand All @@ -405,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 @@ -438,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 All @@ -448,7 +449,7 @@ class AssertionError extends Error {
if (res.length === 1) {
super(`${base} ${res[0]}`);
} else {
super(`${base}\n\n ${res.join('\n ')}\n`);
super(`${base}\n\n${res.join('\n')}\n`);
}
} else {
let res = util.inspect(actual);
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,13 @@ assert.throws(
});

// notDeepEqual tests
message = 'Identical input passed to notDeepStrictEqual:\n\n' +
' [\n 1\n ]\n';
message = 'Identical input passed to notDeepStrictEqual:\n\n[\n 1\n]\n';
assert.throws(
() => assert.notDeepEqual([1], [1]),
{ message });

message = 'Identical input passed to notDeepStrictEqual:' +
`\n\n [${'\n 1,'.repeat(25)}\n ...\n`;
`\n\n[${'\n 1,'.repeat(25)}\n...\n`;
const data = Array(31).fill(1);
assert.throws(
() => assert.notDeepEqual(data, data),
Expand Down Expand Up @@ -901,7 +900,7 @@ assert.throws(() => { throw null; }, 'foo');
assert.throws(
() => assert.strictEqual([], []),
{
message: 'Input object identical but not reference equal:\n\n []\n'
message: 'Input objects identical but not reference equal:\n\n[]\n'
}
);

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