From efdb32603c6578e273632d5ddbcca3e31e9f188e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 10 Sep 2018 08:58:46 +0200 Subject: [PATCH] assert: improve diff output The output is now a tiny bit improved by sorting object properties when inspecting the values that are compared with each other. That reduces the overall diff for identical objects with a different property insertion order. Backport-PR-URL: https://github.com/nodejs/node/pull/23226 PR-URL: https://github.com/nodejs/node/pull/22788 Refs: https://github.com/nodejs/node/issues/22763 Reviewed-By: John-David Dalton Reviewed-By: Anna Henningsen Reviewed-By: Trivikram Kamat --- lib/internal/assert.js | 3 ++- test/parallel/test-assert.js | 39 ++++++++++++++++++--------- test/parallel/test-internal-errors.js | 2 +- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/internal/assert.js b/lib/internal/assert.js index 74f7e3f7c053c4..2e91b08006b242 100644 --- a/lib/internal/assert.js +++ b/lib/internal/assert.js @@ -43,7 +43,8 @@ function inspectValue(val) { // comparison. breakLength: Infinity, // Assert does not detect proxies currently. - showProxy: false + showProxy: false, + sorted: true } ).split('\n'); } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 920d7cea659644..48224eb6b1deb3 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -285,7 +285,7 @@ testAssertionMessage(/abc/gim, '/abc/gim'); testAssertionMessage(function f() {}, '[Function: f]'); testAssertionMessage(function() {}, '[Function]'); testAssertionMessage({}, '{}'); -testAssertionMessage(circular, '{\n- y: 1,\n- x: [Circular]\n- }'); +testAssertionMessage(circular, '{\n- x: [Circular],\n- y: 1\n- }'); testAssertionMessage({ a: undefined, b: null }, '{\n- a: undefined,\n- b: null\n- }'); testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity }, @@ -593,8 +593,8 @@ assert.throws( '\n' + '- {}\n' + '+ {\n' + - "+ loop: 'forever',\n" + - '+ [Symbol(nodejs.util.inspect.custom)]: [Function]\n' + + '+ [Symbol(nodejs.util.inspect.custom)]: [Function],\n' + + "+ loop: 'forever'\n" + '+ }' }); @@ -858,9 +858,12 @@ common.expectsError( code: 'ERR_ASSERTION', name: 'AssertionError [ERR_ASSERTION]', message: `${start}\n${actExp}\n\n` + - " Comparison {\n name: 'TypeError',\n" + - " message: 'Wrong value',\n- code: 404\n" + - '+ code: 404,\n+ foo: undefined\n }' + ' Comparison {\n' + + ' code: 404,\n' + + '+ foo: undefined,\n' + + " message: 'Wrong value',\n" + + " name: 'TypeError'\n" + + ' }' } ); @@ -872,9 +875,13 @@ common.expectsError( code: 'ERR_ASSERTION', name: 'AssertionError [ERR_ASSERTION]', message: `${start}\n${actExp}\n\n` + - " Comparison {\n name: 'TypeError',\n" + - " message: 'Wrong value',\n- code: 404\n" + - "+ code: '404',\n+ foo: undefined\n }" + ' Comparison {\n' + + '- code: 404,\n' + + "+ code: '404',\n" + + '+ foo: undefined,\n' + + " message: 'Wrong value',\n" + + " name: 'TypeError'\n" + + ' }' } ); @@ -904,8 +911,11 @@ common.expectsError( name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', message: `${start}\n${actExp}\n\n` + - " Comparison {\n- name: 'TypeError',\n+ name: 'Error'," + - "\n message: 'e'\n }" + ' Comparison {\n' + + " message: 'e',\n" + + "- name: 'TypeError'\n" + + "+ name: 'Error'\n" + + ' }' } ); assert.throws( @@ -915,8 +925,11 @@ common.expectsError( code: 'ERR_ASSERTION', generatedMessage: true, message: `${start}\n${actExp}\n\n` + - " Comparison {\n name: 'Error',\n- message: 'foo'" + - "\n+ message: ''\n }" + ' Comparison {\n' + + "- message: 'foo',\n" + + "+ message: '',\n" + + " name: 'Error'\n" + + ' }' } ); diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index d4f6a443d82f4c..de5d6ed3b9bb8e 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -92,7 +92,7 @@ common.expectsError(() => { }, { code: 'ERR_ASSERTION', type: assert.AssertionError, - message: /- message: 'Error for testing purposes: a'\n\+ message: \/\^Error/ + message: /- message: 'Error for testing purposes: a',\n\+ message: \/\^Error/ }); // Test ERR_INVALID_FD_TYPE