☕️ Classic, reliable, trusted test framework for Node.js and the browser
Fix diffs of circular structures (close #1198, fix #1179)
Travis Jeffery365dcd2600fe
diff --git a/lib/reporters/base.js b/lib/reporters/base.jsindex 0754fe1a..2690fa10 100644--- a/lib/reporters/base.js+++ b/lib/reporters/base.js@@ -180,8 +180,8 @@ exports.list = function(failures){// explicitly show diffif (err.showDiff && sameType(actual, expected)) {escape = false;- err.actual = actual = stringify(canonicalize(actual));- err.expected = expected = stringify(canonicalize(expected));+ err.actual = actual = utils.stringify(actual);+ err.expected = expected = utils.stringify(expected);}// actual / expected diff@@ -443,53 +443,6 @@ function colorLines(name, str) {}).join('\n');}-/**- * Stringify `obj`.- *- * @param {Object} obj- * @return {String}- * @api private- */--function stringify(obj) {- if (obj instanceof RegExp) return obj.toString();- return JSON.stringify(obj, null, 2);-}--/**- * Return a new object that has the keys in sorted order.- * @param {Object} obj- * @return {Object}- * @api private- */-- function canonicalize(obj, stack) {- stack = stack || [];-- if (utils.indexOf(stack, obj) !== -1) return obj;-- var canonicalizedObj;-- if ('[object Array]' == {}.toString.call(obj)) {- stack.push(obj);- canonicalizedObj = utils.map(obj, function(item) {- return canonicalize(item, stack);- });- stack.pop();- } else if (typeof obj === 'object' && obj !== null) {- stack.push(obj);- canonicalizedObj = {};- utils.forEach(utils.keys(obj).sort(), function(key) {- canonicalizedObj[key] = canonicalize(obj[key], stack);- });- stack.pop();- } else {- canonicalizedObj = obj;- }-- return canonicalizedObj;- }-/*** Check that a / b have the same type.*@@ -504,4 +457,3 @@ function sameType(a, b) {b = Object.prototype.toString.call(b);return a == b;}-diff --git a/lib/utils.js b/lib/utils.jsindex 45430e28..f9902968 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -300,3 +300,51 @@ exports.highlightTags = function(name) {code[i].innerHTML = highlight(code[i].innerHTML);}};+++/**+ * Stringify `obj`.+ *+ * @param {Object} obj+ * @return {String}+ * @api private+ */++exports.stringify = function(obj) {+ if (obj instanceof RegExp) return obj.toString();+ return JSON.stringify(exports.canonicalize(obj), null, 2);+}++/**+ * Return a new object that has the keys in sorted order.+ * @param {Object} obj+ * @return {Object}+ * @api private+ */++exports.canonicalize = function(obj, stack) {+ stack = stack || [];++ if (exports.indexOf(stack, obj) !== -1) return '[Circular]';++ var canonicalizedObj;++ if ({}.toString.call(obj) === '[object Array]') {+ stack.push(obj);+ canonicalizedObj = exports.map(obj, function(item) {+ return exports.canonicalize(item, stack);+ });+ stack.pop();+ } else if (typeof obj === 'object' && obj !== null) {+ stack.push(obj);+ canonicalizedObj = {};+ exports.forEach(exports.keys(obj).sort(), function(key) {+ canonicalizedObj[key] = exports.canonicalize(obj[key], stack);+ });+ stack.pop();+ } else {+ canonicalizedObj = obj;+ }++ return canonicalizedObj;+ }diff --git a/test/acceptance/utils.js b/test/acceptance/utils.jsindex cc494250..1ebdb296 100644--- a/test/acceptance/utils.js+++ b/test/acceptance/utils.js@@ -70,4 +70,20 @@ describe('lib/utils', function () {utils.clean(fn).should.equal("foo()");});});-});\ No newline at end of file++ describe('stringify', function(){+ it('should canoncalize the object', function(){+ var travis = { name: 'travis', age: 24 };+ var travis2 = { age: 24, name: 'travis' };++ utils.stringify(travis).should.equal(utils.stringify(travis2));+ });++ it('should handle circular structures', function(){+ var travis = { name: 'travis' };+ travis.whoami = travis;++ utils.stringify(travis).should.equal('{\n "name": "travis",\n "whoami": "[Circular]"\n}');+ });+ });+});