Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: show the diff when deep comparing data with a custom message #54759

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
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
Loading