mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
assert: show diff when doing partial comparisons
PR-URL: https://github.com/nodejs/node/pull/56211 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
1 parent
ea493c18b2
commit
799d9204f2
6 files changed
+95
-10
No files matched your search
+3
-4
@@ -639,10 +639,9 @@ function compareBranch(
|
||||
// Check if all expected keys and values match
|
||||
for (let i = 0; i < keysExpected.length; i++) {
|
||||
const key = keysExpected[i];
|
||||
assert(
|
||||
ReflectHas(actual, key),
|
||||
new AssertionError({ message: `Expected key ${String(key)} not found in actual object` }),
|
||||
);
|
||||
if (!ReflectHas(actual, key)) {
|
||||
return false;
|
||||
}
|
||||
if (!compareBranch(actual[key], expected[key], comparedObjects)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ const { myersDiff, printMyersDiff, printSimpleMyersDiff } = require('internal/as
|
||||
|
||||
const kReadableOperator = {
|
||||
deepStrictEqual: 'Expected values to be strictly deep-equal:',
|
||||
partialDeepStrictEqual: 'Expected values to be partially and strictly deep-equal:',
|
||||
strictEqual: 'Expected values to be strictly equal:',
|
||||
strictEqualObject: 'Expected "actual" to be reference-equal to "expected":',
|
||||
deepEqual: 'Expected values to be loosely deep-equal:',
|
||||
@@ -41,6 +42,8 @@ const kReadableOperator = {
|
||||
const kMaxShortStringLength = 12;
|
||||
const kMaxLongStringLength = 512;
|
||||
|
||||
const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
|
||||
|
||||
function copyError(source) {
|
||||
const target = ObjectAssign(
|
||||
{ __proto__: ObjectGetPrototypeOf(source) },
|
||||
@@ -210,9 +213,13 @@ function createErrDiff(actual, expected, operator, customMessage) {
|
||||
const checkCommaDisparity = actual != null && typeof actual === 'object';
|
||||
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);
|
||||
|
||||
const myersDiffMessage = printMyersDiff(diff);
|
||||
const myersDiffMessage = printMyersDiff(diff, operator);
|
||||
message = myersDiffMessage.message;
|
||||
|
||||
if (operator === 'partialDeepStrictEqual') {
|
||||
header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`;
|
||||
}
|
||||
|
||||
if (myersDiffMessage.skipped) {
|
||||
skipped = true;
|
||||
}
|
||||
@@ -255,7 +262,7 @@ class AssertionError extends Error {
|
||||
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
|
||||
|
||||
if (message != null) {
|
||||
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
|
||||
if (kMethodsWithCustomMessageDiff.includes(operator)) {
|
||||
super(createErrDiff(actual, expected, operator, message));
|
||||
} else {
|
||||
super(String(message));
|
||||
@@ -275,7 +282,7 @@ class AssertionError extends Error {
|
||||
expected = copyError(expected);
|
||||
}
|
||||
|
||||
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
|
||||
if (kMethodsWithCustomMessageDiff.includes(operator)) {
|
||||
super(createErrDiff(actual, expected, operator, message));
|
||||
} else if (operator === 'notDeepStrictEqual' ||
|
||||
operator === 'notStrictEqual') {
|
||||
|
||||
@@ -122,7 +122,7 @@ function printSimpleMyersDiff(diff) {
|
||||
return `\n${message}`;
|
||||
}
|
||||
|
||||
function printMyersDiff(diff, simple = false) {
|
||||
function printMyersDiff(diff, operator) {
|
||||
let message = '';
|
||||
let skipped = false;
|
||||
let nopCount = 0;
|
||||
@@ -148,7 +148,11 @@ function printMyersDiff(diff, simple = false) {
|
||||
}
|
||||
|
||||
if (type === 'insert') {
|
||||
message += `${colors.green}+${colors.white} ${value}\n`;
|
||||
if (operator === 'partialDeepStrictEqual') {
|
||||
message += `${colors.gray}${colors.hasColors ? ' ' : '+'} ${value}${colors.white}\n`;
|
||||
} else {
|
||||
message += `${colors.green}+${colors.white} ${value}\n`;
|
||||
}
|
||||
} else if (type === 'delete') {
|
||||
message += `${colors.red}-${colors.white} ${value}\n`;
|
||||
} else if (type === 'nop') {
|
||||
|
||||
@@ -1351,6 +1351,17 @@ test('Additional assert', () => {
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
assert.partialDeepStrictEqual({ 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 }\n'
|
||||
}
|
||||
);
|
||||
|
||||
{
|
||||
let threw = false;
|
||||
try {
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
// Flags: --no-warnings
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert').strict;
|
||||
|
||||
assert.throws(() => {
|
||||
function setup() {
|
||||
process.env.FORCE_COLOR = '1';
|
||||
delete process.env.NODE_DISABLE_COLORS;
|
||||
delete process.env.NO_COLOR;
|
||||
}
|
||||
|
||||
assert.throws(() => {
|
||||
setup();
|
||||
assert.deepStrictEqual([1, 2, 2, 2, 2], [2, 2, 2, 2, 2]);
|
||||
}, (err) => {
|
||||
const expected = 'Expected values to be strictly deep-equal:\n' +
|
||||
@@ -19,6 +24,48 @@ assert.throws(() => {
|
||||
'\x1B[39m 2,\n' +
|
||||
'\x1B[31m-\x1B[39m 2\n' +
|
||||
'\x1B[39m ]\n';
|
||||
|
||||
assert.strictEqual(err.message, expected);
|
||||
return true;
|
||||
});
|
||||
|
||||
{
|
||||
assert.throws(() => {
|
||||
setup();
|
||||
assert.partialDeepStrictEqual({ a: 1, b: 2, c: 3, d: 5 }, { z: 4, b: 5 });
|
||||
}, (err) => {
|
||||
const expected = 'Expected values to be partially and strictly deep-equal:\n' +
|
||||
'\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' +
|
||||
'\n' +
|
||||
'\x1B[39m {\n' +
|
||||
'\x1B[90m a: 1,\x1B[39m\n' +
|
||||
'\x1B[90m b: 2,\x1B[39m\n' +
|
||||
'\x1B[90m c: 3,\x1B[39m\n' +
|
||||
'\x1B[90m d: 5\x1B[39m\n' +
|
||||
'\x1B[31m-\x1B[39m b: 5,\n' +
|
||||
'\x1B[31m-\x1B[39m z: 4\n' +
|
||||
'\x1B[39m }\n';
|
||||
|
||||
assert.strictEqual(err.message, expected);
|
||||
return true;
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
setup();
|
||||
assert.partialDeepStrictEqual([1, 2, 3, 5], [4, 5]);
|
||||
}, (err) => {
|
||||
const expected = 'Expected values to be partially and strictly deep-equal:\n' +
|
||||
'\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' +
|
||||
'\n' +
|
||||
'\x1B[39m [\n' +
|
||||
'\x1B[90m 1,\x1B[39m\n' +
|
||||
'\x1B[90m 2,\x1B[39m\n' +
|
||||
'\x1B[90m 3,\x1B[39m\n' +
|
||||
'\x1B[31m-\x1B[39m 4,\n' +
|
||||
'\x1B[39m 5\n' +
|
||||
'\x1B[39m ]\n';
|
||||
|
||||
assert.strictEqual(err.message, expected);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// Flags: --no-warnings
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert').strict;
|
||||
@@ -17,3 +18,19 @@ assert.throws(
|
||||
'- foo: \'bar\'\n' +
|
||||
'- }\n',
|
||||
});
|
||||
|
||||
{
|
||||
assert.throws(
|
||||
() => {
|
||||
assert.partialDeepStrictEqual({}, { foo: 'bar' });
|
||||
},
|
||||
{
|
||||
message: 'Expected values to be partially and strictly deep-equal:\n' +
|
||||
'+ actual - expected\n' +
|
||||
'\n' +
|
||||
'+ {}\n' +
|
||||
'- {\n' +
|
||||
"- foo: 'bar'\n" +
|
||||
'- }\n',
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user