diff --git a/lib/assert.js b/lib/assert.js index 9ca871da3d..3f35b4510e 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -51,26 +51,37 @@ assert.AssertionError = function AssertionError(options) { }; util.inherits(assert.AssertionError, Error); +function replacer(key, value) { + if (value === undefined) { + return '' + value; + } + if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) { + return value.toString(); + } + if (typeof value === 'function' || value instanceof RegExp) { + return value.toString(); + } + return value; +} + +function truncate(s, n) { + if (typeof s == 'string') { + return s.length < n ? s : s.slice(0, n); + } else { + return s; + } +} + assert.AssertionError.prototype.toString = function() { if (this.message) { - return [this.name + ':', this.message].join(' '); + return [ this.name + ':', this.message ].join(' '); } else { - return [this.name + ':', - JSON.stringify(this.expected, replacer), - this.operator, - JSON.stringify(this.actual, replacer)].join(' '); - } - function replacer(key, value) { - if (value === undefined) { - return '' + value; - } - if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) { - return value.toString(); - } - if (typeof value === 'function' || value instanceof RegExp) { - return value.toString(); - } - return value; + return [ + this.name + ':', + truncate(JSON.stringify(this.actual, replacer), 128), + this.operator, + truncate(JSON.stringify(this.expected, replacer), 128) + ].join(' '); } }; diff --git a/test/simple/test-assert.js b/test/simple/test-assert.js index b47f045454..f0e78b85f6 100644 --- a/test/simple/test-assert.js +++ b/test/simple/test-assert.js @@ -236,7 +236,7 @@ function testAssertionMessage(actual, expected) { assert.equal(actual, ''); } catch (e) { assert.equal(e.toString(), - ['AssertionError:', '""', '==', expected].join(' ')); + ['AssertionError:', expected, '==', '""'].join(' ')); } } testAssertionMessage(undefined, '"undefined"');