| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -639,10 +639,9 @@ function compareBranch( | |||
| 639 | 639 | // Check if all expected keys and values match | |
| 640 | 640 | for (let i = 0; i < keysExpected.length; i++) { | |
| 641 | 641 | const key = keysExpected[i]; | |
| 642 | - assert( | ||
| 643 | - ReflectHas(actual, key), | ||
| 644 | - new AssertionError({ message: `Expected key ${String(key)} not found in actual object` }), | ||
| 645 | - ); | ||
| 642 | + if (!ReflectHas(actual, key)) { | ||
| 643 | + return false; | ||
| 644 | + } | ||
| 646 | 645 | if (!compareBranch(actual[key], expected[key], comparedObjects)) { | |
| 647 | 646 | return false; | |
| 648 | 647 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,7 @@ const { myersDiff, printMyersDiff, printSimpleMyersDiff } = require('internal/as | |||
| 26 | 26 | ||
| 27 | 27 | const kReadableOperator = { | |
| 28 | 28 | deepStrictEqual: 'Expected values to be strictly deep-equal:', | |
| 29 | + partialDeepStrictEqual: 'Expected values to be partially and strictly deep-equal:', | ||
| 29 | 30 | strictEqual: 'Expected values to be strictly equal:', | |
| 30 | 31 | strictEqualObject: 'Expected "actual" to be reference-equal to "expected":', | |
| 31 | 32 | deepEqual: 'Expected values to be loosely deep-equal:', | |
@@ -41,6 +42,8 @@ const kReadableOperator = { | |||
| 41 | 42 | const kMaxShortStringLength = 12; | |
| 42 | 43 | const kMaxLongStringLength = 512; | |
| 43 | 44 | ||
| 45 | + const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual']; | ||
| 46 | + | ||
| 44 | 47 | function copyError(source) { | |
| 45 | 48 | const target = ObjectAssign( | |
| 46 | 49 | { __proto__: ObjectGetPrototypeOf(source) }, | |
@@ -210,9 +213,13 @@ function createErrDiff(actual, expected, operator, customMessage) { | |||
| 210 | 213 | const checkCommaDisparity = actual != null && typeof actual === 'object'; | |
| 211 | 214 | const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity); | |
| 212 | 215 | ||
| 213 | - const myersDiffMessage = printMyersDiff(diff); | ||
| 216 | + const myersDiffMessage = printMyersDiff(diff, operator); | ||
| 214 | 217 | message = myersDiffMessage.message; | |
| 215 | 218 | ||
| 219 | + if (operator === 'partialDeepStrictEqual') { | ||
| 220 | + header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`; | ||
| 221 | + } | ||
| 222 | + | ||
| 216 | 223 | if (myersDiffMessage.skipped) { | |
| 217 | 224 | skipped = true; | |
| 218 | 225 | } | |
@@ -255,7 +262,7 @@ class AssertionError extends Error { | |||
| 255 | 262 | if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0; | |
| 256 | 263 | ||
| 257 | 264 | if (message != null) { | |
| 258 | - if (operator === 'deepStrictEqual' || operator === 'strictEqual') { | ||
| 265 | + if (kMethodsWithCustomMessageDiff.includes(operator)) { | ||
| 259 | 266 | super(createErrDiff(actual, expected, operator, message)); | |
| 260 | 267 | } else { | |
| 261 | 268 | super(String(message)); | |
@@ -275,7 +282,7 @@ class AssertionError extends Error { | |||
| 275 | 282 | expected = copyError(expected); | |
| 276 | 283 | } | |
| 277 | 284 | ||
| 278 | - if (operator === 'deepStrictEqual' || operator === 'strictEqual') { | ||
| 285 | + if (kMethodsWithCustomMessageDiff.includes(operator)) { | ||
| 279 | 286 | super(createErrDiff(actual, expected, operator, message)); | |
| 280 | 287 | } else if (operator === 'notDeepStrictEqual' || | |
| 281 | 288 | operator === 'notStrictEqual') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,7 +122,7 @@ function printSimpleMyersDiff(diff) { | |||
| 122 | 122 | return `\n${message}`; | |
| 123 | 123 | } | |
| 124 | 124 | ||
| 125 | - function printMyersDiff(diff, simple = false) { | ||
| 125 | + function printMyersDiff(diff, operator) { | ||
| 126 | 126 | let message = ''; | |
| 127 | 127 | let skipped = false; | |
| 128 | 128 | let nopCount = 0; | |
@@ -148,7 +148,11 @@ function printMyersDiff(diff, simple = false) { | |||
| 148 | 148 | } | |
| 149 | 149 | ||
| 150 | 150 | if (type === 'insert') { | |
| 151 | - message += `${colors.green}+${colors.white} ${value}\n`; | ||
| 151 | + if (operator === 'partialDeepStrictEqual') { | ||
| 152 | + message += `${colors.gray}${colors.hasColors ? ' ' : '+'} ${value}${colors.white}\n`; | ||
| 153 | + } else { | ||
| 154 | + message += `${colors.green}+${colors.white} ${value}\n`; | ||
| 155 | + } | ||
| 152 | 156 | } else if (type === 'delete') { | |
| 153 | 157 | message += `${colors.red}-${colors.white} ${value}\n`; | |
| 154 | 158 | } else if (type === 'nop') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1351,6 +1351,17 @@ test('Additional assert', () => { | |||
| 1351 | 1351 | } | |
| 1352 | 1352 | ); | |
| 1353 | 1353 | ||
| 1354 | + assert.throws( | ||
| 1355 | + () => { | ||
| 1356 | + assert.partialDeepStrictEqual({ a: true }, { a: false }, 'custom message'); | ||
| 1357 | + }, | ||
| 1358 | + { | ||
| 1359 | + code: 'ERR_ASSERTION', | ||
| 1360 | + name: 'AssertionError', | ||
| 1361 | + message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }\n' | ||
| 1362 | + } | ||
| 1363 | + ); | ||
| 1364 | + | ||
| 1354 | 1365 | { | |
| 1355 | 1366 | let threw = false; | |
| 1356 | 1367 | try { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,16 @@ | |||
| 1 | + // Flags: --no-warnings | ||
| 1 | 2 | 'use strict'; | |
| 2 | 3 | require('../common'); | |
| 3 | 4 | const assert = require('assert').strict; | |
| 4 | 5 | ||
| 5 | - assert.throws(() => { | ||
| 6 | + function setup() { | ||
| 6 | 7 | process.env.FORCE_COLOR = '1'; | |
| 7 | 8 | delete process.env.NODE_DISABLE_COLORS; | |
| 8 | 9 | delete process.env.NO_COLOR; | |
| 10 | + } | ||
| 11 | + | ||
| 12 | + assert.throws(() => { | ||
| 13 | + setup(); | ||
| 9 | 14 | assert.deepStrictEqual([1, 2, 2, 2, 2], [2, 2, 2, 2, 2]); | |
| 10 | 15 | }, (err) => { | |
| 11 | 16 | const expected = 'Expected values to be strictly deep-equal:\n' + | |
@@ -19,6 +24,48 @@ assert.throws(() => { | |||
| 19 | 24 | '\x1B[39m 2,\n' + | |
| 20 | 25 | '\x1B[31m-\x1B[39m 2\n' + | |
| 21 | 26 | '\x1B[39m ]\n'; | |
| 27 | + | ||
| 22 | 28 | assert.strictEqual(err.message, expected); | |
| 23 | 29 | return true; | |
| 24 | 30 | }); | |
| 31 | + | ||
| 32 | + { | ||
| 33 | + assert.throws(() => { | ||
| 34 | + setup(); | ||
| 35 | + assert.partialDeepStrictEqual({ a: 1, b: 2, c: 3, d: 5 }, { z: 4, b: 5 }); | ||
| 36 | + }, (err) => { | ||
| 37 | + const expected = 'Expected values to be partially and strictly deep-equal:\n' + | ||
| 38 | + '\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' + | ||
| 39 | + '\n' + | ||
| 40 | + '\x1B[39m {\n' + | ||
| 41 | + '\x1B[90m a: 1,\x1B[39m\n' + | ||
| 42 | + '\x1B[90m b: 2,\x1B[39m\n' + | ||
| 43 | + '\x1B[90m c: 3,\x1B[39m\n' + | ||
| 44 | + '\x1B[90m d: 5\x1B[39m\n' + | ||
| 45 | + '\x1B[31m-\x1B[39m b: 5,\n' + | ||
| 46 | + '\x1B[31m-\x1B[39m z: 4\n' + | ||
| 47 | + '\x1B[39m }\n'; | ||
| 48 | + | ||
| 49 | + assert.strictEqual(err.message, expected); | ||
| 50 | + return true; | ||
| 51 | + }); | ||
| 52 | + | ||
| 53 | + assert.throws(() => { | ||
| 54 | + setup(); | ||
| 55 | + assert.partialDeepStrictEqual([1, 2, 3, 5], [4, 5]); | ||
| 56 | + }, (err) => { | ||
| 57 | + const expected = 'Expected values to be partially and strictly deep-equal:\n' + | ||
| 58 | + '\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' + | ||
| 59 | + '\n' + | ||
| 60 | + '\x1B[39m [\n' + | ||
| 61 | + '\x1B[90m 1,\x1B[39m\n' + | ||
| 62 | + '\x1B[90m 2,\x1B[39m\n' + | ||
| 63 | + '\x1B[90m 3,\x1B[39m\n' + | ||
| 64 | + '\x1B[31m-\x1B[39m 4,\n' + | ||
| 65 | + '\x1B[39m 5\n' + | ||
| 66 | + '\x1B[39m ]\n'; | ||
| 67 | + | ||
| 68 | + assert.strictEqual(err.message, expected); | ||
| 69 | + return true; | ||
| 70 | + }); | ||
| 71 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + // Flags: --no-warnings | ||
| 1 | 2 | 'use strict'; | |
| 2 | 3 | require('../common'); | |
| 3 | 4 | const assert = require('assert').strict; | |
@@ -17,3 +18,19 @@ assert.throws( | |||
| 17 | 18 | '- foo: \'bar\'\n' + | |
| 18 | 19 | '- }\n', | |
| 19 | 20 | }); | |
| 21 | + | ||
| 22 | + { | ||
| 23 | + assert.throws( | ||
| 24 | + () => { | ||
| 25 | + assert.partialDeepStrictEqual({}, { foo: 'bar' }); | ||
| 26 | + }, | ||
| 27 | + { | ||
| 28 | + message: 'Expected values to be partially and strictly deep-equal:\n' + | ||
| 29 | + '+ actual - expected\n' + | ||
| 30 | + '\n' + | ||
| 31 | + '+ {}\n' + | ||
| 32 | + '- {\n' + | ||
| 33 | + "- foo: 'bar'\n" + | ||
| 34 | + '- }\n', | ||
| 35 | + }); | ||
| 36 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments