Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit bcff90e

Browse files
cjihrigJulien Gilli
authored andcommitted
assert: use util.inspect() to create error messages
Currently, JSON.stringify() is used to create error messages on failed assertions. This causes an error when stringifying objects with circular references. This commit switches out JSON.stringify() for util.inspect(), which can handle circular references. PR: #8734 PR-URL: #8734 Reviewed-By: Julien Gilli <[email protected]>
1 parent 3b392d3 commit bcff90e

2 files changed

Lines changed: 19 additions & 28 deletions

File tree

lib/assert.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,6 @@ assert.AssertionError = function AssertionError(options) {
5757
// assert.AssertionError instanceof Error
5858
util.inherits(assert.AssertionError, Error);
5959

60-
function replacer(key, value) {
61-
if (util.isUndefined(value)) {
62-
return '' + value;
63-
}
64-
if (util.isNumber(value) && !isFinite(value)) {
65-
return value.toString();
66-
}
67-
if (util.isFunction(value) || util.isRegExp(value)) {
68-
return value.toString();
69-
}
70-
return value;
71-
}
72-
7360
function truncate(s, n) {
7461
if (util.isString(s)) {
7562
return s.length < n ? s : s.slice(0, n);
@@ -79,9 +66,9 @@ function truncate(s, n) {
7966
}
8067

8168
function getMessage(self) {
82-
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
69+
return truncate(util.inspect(self.actual, {depth: null}), 128) + ' ' +
8370
self.operator + ' ' +
84-
truncate(JSON.stringify(self.expected, replacer), 128);
71+
truncate(util.inspect(self.expected, {depth: null}), 128);
8572
}
8673

8774
// At present only the three keys mentioned above are used and

test/simple/test-assert.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,36 +258,40 @@ console.log('All OK');
258258
assert.ok(gotError);
259259

260260

261-
// #217
261+
var circular = {y: 1};
262+
circular.x = circular;
263+
262264
function testAssertionMessage(actual, expected) {
263265
try {
264266
assert.equal(actual, '');
265267
} catch (e) {
266268
assert.equal(e.toString(),
267-
['AssertionError:', expected, '==', '""'].join(' '));
269+
['AssertionError:', expected, '==', '\'\''].join(' '));
268270
assert.ok(e.generatedMessage, "Message not marked as generated");
269271
}
270272
}
271-
testAssertionMessage(undefined, '"undefined"');
273+
testAssertionMessage(undefined, 'undefined');
272274
testAssertionMessage(null, 'null');
273275
testAssertionMessage(true, 'true');
274276
testAssertionMessage(false, 'false');
275277
testAssertionMessage(0, '0');
276278
testAssertionMessage(100, '100');
277-
testAssertionMessage(NaN, '"NaN"');
278-
testAssertionMessage(Infinity, '"Infinity"');
279-
testAssertionMessage(-Infinity, '"-Infinity"');
279+
testAssertionMessage(NaN, 'NaN');
280+
testAssertionMessage(Infinity, 'Infinity');
281+
testAssertionMessage(-Infinity, '-Infinity');
280282
testAssertionMessage('', '""');
281-
testAssertionMessage('foo', '"foo"');
283+
testAssertionMessage('foo', '\'foo\'');
282284
testAssertionMessage([], '[]');
283-
testAssertionMessage([1, 2, 3], '[1,2,3]');
284-
testAssertionMessage(/a/, '"/a/"');
285-
testAssertionMessage(/abc/gim, '"/abc/gim"');
286-
testAssertionMessage(function f() {}, '"function f() {}"');
285+
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
286+
testAssertionMessage(/a/, '/a/');
287+
testAssertionMessage(/abc/gim, '/abc/gim');
288+
testAssertionMessage(function f() {}, '[Function: f]');
289+
testAssertionMessage(function () {}, '[Function]');
287290
testAssertionMessage({}, '{}');
288-
testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');
291+
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
292+
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
289293
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
290-
'{"a":"NaN","b":"Infinity","c":"-Infinity"}');
294+
'{ a: NaN, b: Infinity, c: -Infinity }');
291295

292296
// #2893
293297
try {

0 commit comments

Comments
 (0)