Skip to content

Commit

Permalink
assert: show the diff when deep comparing data with a custom message
Browse files Browse the repository at this point in the history
Fixes: #48465
PR-URL: #54759
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
puskin94 authored and jasnell committed Sep 28, 2024
1 parent 6d1cd50 commit e1d8b4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 12 additions & 5 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ function inspectValue(val) {
);
}

function createErrDiff(actual, expected, operator) {
function getErrorMessage(operator, message) {
return message || kReadableOperator[operator];
}

function createErrDiff(actual, expected, operator, message = '') {
let other = '';
let res = '';
let end = '';
Expand Down Expand Up @@ -110,7 +114,7 @@ function createErrDiff(actual, expected, operator) {
if ((typeof actual !== 'object' || actual === null) &&
(typeof expected !== 'object' || expected === null) &&
(actual !== 0 || expected !== 0)) { // -0 === +0
return `${kReadableOperator[operator]}\n\n` +
return `${getErrorMessage(operator, message)}\n\n` +
`${actualLines[0]} !== ${expectedLines[0]}\n`;
}
} else if (operator !== 'strictEqualObject') {
Expand Down Expand Up @@ -184,8 +188,7 @@ function createErrDiff(actual, expected, operator) {

let printedLines = 0;
let identical = 0;
const msg = kReadableOperator[operator] +
`\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
const msg = `${getErrorMessage(operator, message)}\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
const skippedMsg = ` ${colors.blue}...${colors.white} Lines skipped`;

let lines = actualLines;
Expand Down Expand Up @@ -337,7 +340,11 @@ class AssertionError extends Error {
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;

if (message != null) {
super(String(message));
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
super(createErrDiff(actual, expected, operator, message));
} else {
super(String(message));
}
} else {
// Reset colors on each call to make sure we handle dynamically set environment
// variables correct.
Expand Down
15 changes: 13 additions & 2 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ test('Test assertion messages', () => {
assert.throws(
() => assert.strictEqual(1, 2, 'oh no'),
{
message: 'oh no',
message: 'oh no\n\n1 !== 2\n',
generatedMessage: false
}
);
Expand Down Expand Up @@ -1204,7 +1204,7 @@ test('Additional assert', () => {
),
{
actual,
message,
message: "message\n+ actual - expected\n\n+ 'foobar'\n- {\n- message: 'foobar'\n- }",
operator: 'throws',
generatedMessage: false
}
Expand Down Expand Up @@ -1251,6 +1251,17 @@ test('Additional assert', () => {
}
);

assert.throws(
() => {
assert.deepStrictEqual({ a: true }, { a: false }, 'custom message');
},
{
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }'
}
);

{
let threw = false;
try {
Expand Down

0 comments on commit e1d8b4f

Please sign in to comment.