Skip to content

Commit 40e29dc

Browse files
committed
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-URL: #668 Reviewed-By: Julien Gilli <julien.gilli@joyent.com> Reviewed-By: Bert Belder <bertbelder@gmail.com> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
1 parent bc2c85c commit 40e29dc

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

lib/assert.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ assert.AssertionError = function AssertionError(options) {
5858
// assert.AssertionError instanceof Error
5959
util.inherits(assert.AssertionError, Error);
6060

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

8269
function getMessage(self) {
83-
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
70+
return truncate(util.inspect(self.actual), 128) + ' ' +
8471
self.operator + ' ' +
85-
truncate(JSON.stringify(self.expected, replacer), 128);
72+
truncate(util.inspect(self.expected), 128);
8673
}
8774

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

test/parallel/test-assert.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -249,36 +249,41 @@ a.throws(makeBlock(a.deepEqual, args, []));
249249
assert.ok(gotError);
250250

251251

252-
// #217
252+
var circular = {y: 1};
253+
circular.x = circular;
254+
253255
function testAssertionMessage(actual, expected) {
254256
try {
255257
assert.equal(actual, '');
256258
} catch (e) {
257259
assert.equal(e.toString(),
258-
['AssertionError:', expected, '==', '""'].join(' '));
260+
['AssertionError:', expected, '==', '\'\''].join(' '));
259261
assert.ok(e.generatedMessage, "Message not marked as generated");
260262
}
261263
}
262-
testAssertionMessage(undefined, '"undefined"');
264+
265+
testAssertionMessage(undefined, 'undefined');
263266
testAssertionMessage(null, 'null');
264267
testAssertionMessage(true, 'true');
265268
testAssertionMessage(false, 'false');
266269
testAssertionMessage(0, '0');
267270
testAssertionMessage(100, '100');
268-
testAssertionMessage(NaN, '"NaN"');
269-
testAssertionMessage(Infinity, '"Infinity"');
270-
testAssertionMessage(-Infinity, '"-Infinity"');
271+
testAssertionMessage(NaN, 'NaN');
272+
testAssertionMessage(Infinity, 'Infinity');
273+
testAssertionMessage(-Infinity, '-Infinity');
271274
testAssertionMessage('', '""');
272-
testAssertionMessage('foo', '"foo"');
275+
testAssertionMessage('foo', '\'foo\'');
273276
testAssertionMessage([], '[]');
274-
testAssertionMessage([1, 2, 3], '[1,2,3]');
275-
testAssertionMessage(/a/, '"/a/"');
276-
testAssertionMessage(/abc/gim, '"/abc/gim"');
277-
testAssertionMessage(function f() {}, '"function f() {}"');
277+
testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
278+
testAssertionMessage(/a/, '/a/');
279+
testAssertionMessage(/abc/gim, '/abc/gim');
280+
testAssertionMessage(function f() {}, '[Function: f]');
281+
testAssertionMessage(function () {}, '[Function]');
278282
testAssertionMessage({}, '{}');
279-
testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');
283+
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
284+
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
280285
testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
281-
'{"a":"NaN","b":"Infinity","c":"-Infinity"}');
286+
'{ a: NaN, b: Infinity, c: -Infinity }');
282287

283288
// #2893
284289
try {

0 commit comments

Comments
 (0)