Skip to content
Browse files

Improvements AssertionError message

Fixes #217.
  • Loading branch information...
1 parent 7e8735b commit 84dd95f545242b47475318cec2e267985cee0d1d @koichik committed
Showing with 45 additions and 2 deletions.
  1. +14 −2 lib/assert.js
  2. +31 −0 test/simple/test-assert.js
View
16 lib/assert.js
@@ -56,9 +56,21 @@ assert.AssertionError.prototype.toString = function() {
return [this.name + ':', this.message].join(' ');
} else {
return [this.name + ':',
- JSON.stringify(this.expected),
+ JSON.stringify(this.expected, replacer),
this.operator,
- JSON.stringify(this.actual)].join(' ');
+ 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;
}
};
View
31 test/simple/test-assert.js
@@ -229,3 +229,34 @@ try {
console.log('All OK');
assert.ok(gotError);
+
+// #217
+function testAssertionMessage(actual, expected) {
+ try {
+ assert.equal(actual, '');
+ } catch (e) {
+ assert.equal(e.toString(),
+ ['AssertionError:', '""', '==', expected].join(' '));
+ }
+}
+testAssertionMessage(undefined, '"undefined"');
+testAssertionMessage(null, 'null');
+testAssertionMessage(true, 'true');
+testAssertionMessage(false, 'false');
+testAssertionMessage(0, '0');
+testAssertionMessage(100, '100');
+testAssertionMessage(NaN, '"NaN"');
+testAssertionMessage(Infinity, '"Infinity"');
+testAssertionMessage(-Infinity, '"-Infinity"');
+testAssertionMessage('', '""');
+testAssertionMessage('foo', '"foo"');
+testAssertionMessage([], '[]');
+testAssertionMessage([1,2,3], '[1,2,3]');
+testAssertionMessage(/a/, '"/a/"');
+testAssertionMessage(/abc/gim, '"/abc/gim"');
+testAssertionMessage(function f() {}, '"function f() {}"');
+testAssertionMessage({}, '{}');
+testAssertionMessage({a:undefined, b:null}, '{"a":"undefined","b":null}');
+testAssertionMessage({a:NaN, b:Infinity, c:-Infinity},
+ '{"a":"NaN","b":"Infinity","c":"-Infinity"}');
+

0 comments on commit 84dd95f

Please sign in to comment.
Something went wrong with that request. Please try again.