Skip to content
Open
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
45 changes: 31 additions & 14 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,47 @@ function getColoredMyersDiff(actual, expected) {
function getStackedDiff(actual, expected) {
const isStringComparison = typeof actual === 'string' && typeof expected === 'string';

let message = `\n${colors.green}+${colors.white} ${actual}\n${colors.red}- ${colors.white}${expected}`;
const stringsLen = actual.length + expected.length;
const maxTerminalLength = process.stderr.isTTY ? process.stderr.columns : 80;
const showIndicator = isStringComparison && (stringsLen <= maxTerminalLength);

if (showIndicator) {
let indicatorIdx = -1;
let displayActual = actual;
let displayExpected = expected;
let displayIndicatorIdx = -1;

if (isStringComparison) {
for (let i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) {
// Skip the indicator for the first 2 characters because the diff is immediately apparent
// It is 3 instead of 2 to account for the quotes
if (i >= 3) {
indicatorIdx = i;
}
displayIndicatorIdx = i;
break;
}
}

if (displayIndicatorIdx === -1 && actual.length !== expected.length) {
displayIndicatorIdx = actual.length;
}

if (displayIndicatorIdx !== -1) {
const contextLength = 15;

if (displayIndicatorIdx > contextLength + 3) {
const sliceStart = displayIndicatorIdx - contextLength;
displayActual = `...${StringPrototypeSlice(actual, sliceStart)}`;
displayExpected = `...${StringPrototypeSlice(expected, sliceStart)}`;
displayIndicatorIdx = contextLength + 3;
}

if (indicatorIdx !== -1) {
message += `\n${StringPrototypeRepeat(' ', indicatorIdx + 2)}^`;
if (displayActual.length > displayIndicatorIdx + contextLength + 3) {
displayActual = `${StringPrototypeSlice(displayActual, 0, displayIndicatorIdx + contextLength)}...`;
}
if (displayExpected.length > displayIndicatorIdx + contextLength + 3) {
displayExpected = `${StringPrototypeSlice(displayExpected, 0, displayIndicatorIdx + contextLength)}...`;
}
}
}

let message = `\n${colors.green}+${colors.white} ${displayActual}\n${colors.red}- ${colors.white}${displayExpected}`;

if (isStringComparison && displayIndicatorIdx >= 3) {
message += `\n${StringPrototypeRepeat(' ', displayIndicatorIdx + 2)}^`;
}

return { message };
}

Expand Down
28 changes: 25 additions & 3 deletions lib/internal/assert/myers_diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypePush,
Int32Array,
StringPrototypeEndsWith,
StringPrototypeSlice,
} = primordials;

const {
Expand Down Expand Up @@ -125,19 +126,40 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
}

function printSimpleMyersDiff(diff) {
let message = '';

const grouped = [];
for (let diffIdx = diff.length - 1; diffIdx >= 0; diffIdx--) {
const { 0: operation, 1: value } = diff[diffIdx];
if (grouped.length > 0 && grouped[grouped.length - 1][0] === operation) {
grouped[grouped.length - 1][1] += value;
} else {
ArrayPrototypePush(grouped, [operation, value]);
}
}

let message = '';
const contextLength = 10;

for (let i = 0; i < grouped.length; i++) {
const { 0: operation, 1: value } = grouped[i];
let color = colors.white;
let displayVal = value;

if (operation === kOperations.INSERT) {
color = colors.green;
} else if (operation === kOperations.DELETE) {
color = colors.red;
} else if (operation === kOperations.NOP) {
const startContext = i > 0 ? contextLength : 0;
const endContext = i < grouped.length - 1 ? contextLength : 0;

if (value.length > startContext + endContext + 3) {
displayVal = (startContext ? StringPrototypeSlice(value, 0, startContext) : '') +
`${colors.blue}...${colors.white}` +
(endContext ? StringPrototypeSlice(value, -endContext) : '');
}
}

message += `${color}${value}${colors.white}`;
message += `${color}${displayVal}${colors.white}`;
}

return `\n${message}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test at test/fixtures/test-runner/output/assertion-color-tty.mjs:4:1
[AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
[32mactual[39m [31mexpected[39m

[39m'[39m[32m![39m[39mH[39m[39me[39m[39ml[39m[39ml[39m[39mo[39m[39m [39m[39mW[39m[39mo[39m[39mr[39m[39ml[39m[39md[39m[31m![39m[39m'[39m
[39m'[39m[32m![39m[39mHello World[39m[31m![39m[39m'[39m
] {
generatedMessage: [33mtrue[39m,
code: [32m'ERR_ASSERTION'[39m,
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-assert-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ test('Assert class non strict with full diff', () => {
assertInstance.strictEqual(
err.message,
`Expected values to be strictly equal:\n+ actual - expected\n\n` +
`+ '${longStringOfAs}'\n- '${longStringOfBs}'\n`
`+ '${'A'.repeat(15)}...\n- '${'B'.repeat(15)}...\n`
);
assertInstance.ok(
inspect(err).includes(`actual: '${longLinesOfAsWithEllipsis}'`)
Expand Down Expand Up @@ -341,7 +341,7 @@ test('Assert class non strict with simple diff', () => {
assertInstance.strictEqual(
err.message,
`Expected values to be strictly equal:\n+ actual - expected\n\n` +
`+ '${longStringOfAs}'\n- '${longStringOfBs}'\n`
`+ '${'A'.repeat(15)}...\n- '${'B'.repeat(15)}...\n`
);
assertInstance.ok(
inspect(err).includes(`actual: '${longLinesOfAsWithEllipsis}'`)
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ test('Test assertion messages', () => {
testAssertionMessage(/a/, '/a/');
testAssertionMessage(/abc/gim, '/abc/gim');
testLongAssertionMessage(function f() {}, '[Function: f]');
testLongAssertionMessage(function() {}, '[Function (anonymous)]');
testLongAssertionMessage(function() {}, '[Function (anon...');

assert.throws(
() => assert.strictEqual([1, 2, 3], ''),
Expand Down Expand Up @@ -536,7 +536,7 @@ test('Long values should be truncated for display', () => {
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert.strictEqual(err.message,
`${strictEqualMessageStart}+ actual - expected\n\n` +
`+ '${'A'.repeat(1000)}'\n- ''\n`);
`+ '${'A'.repeat(15)}...\n- ''\n`);
assert.strictEqual(err.actual.length, 1000);
assert.ok(inspect(err).includes(`actual: '${'A'.repeat(488)}...'`));
return true;
Expand Down
Loading