☕️ Classic, reliable, trusted test framework for Node.js and the browser
Base reporter: Canonicalize objects before stringifying and diffing them.
Andreas Lind Petersen138ba985f109
diff --git a/lib/reporters/base.js b/lib/reporters/base.jsindex de17897b..e771695b 100644--- a/lib/reporters/base.js+++ b/lib/reporters/base.js@@ -5,7 +5,8 @@var tty = require('tty'), diff = require('diff')- , ms = require('../ms');+ , ms = require('../ms')+ , utils = require('../utils');/*** Save timer references to avoid Sinon interfering (see GH-237).@@ -179,8 +180,8 @@ exports.list = function(failures){// explicitly show diffif (err.showDiff && sameType(actual, expected)) {escape = false;- err.actual = actual = stringify(actual);- err.expected = expected = stringify(expected);+ err.actual = actual = stringify(canonicalizeObject(actual));+ err.expected = expected = stringify(canonicalizeObject(expected));}// actual / expected diff@@ -455,6 +456,39 @@ function stringify(obj) {return JSON.stringify(obj, null, 2);}+/**+ * Return a new object that has the in sorted order.+ * @param {Mixed} obj+ * @return {Mixed}+ */+function canonicalizeObject(obj, stack) {+ if (!stack) {+ stack = [];+ } else if (utils.indexOf(stack, obj) !== -1) {+ // There's a cycle, bail out+ return obj;+ }+ stack = stack || [];+ if ('[object Array]' == {}.toString.call(obj)) {+ stack.push(obj);+ var returnValue = utils.map(obj, function (item) {+ return canonicalizeObject(item, stack);+ });+ stack.pop();+ return returnValue;+ } else if (typeof obj === 'object' && obj !== null) {+ stack.push(obj);+ var sortedObj = {};+ utils.forEach(utils.keys(obj).sort(), function (key) {+ sortedObj[key] = canonicalizeObject(obj[key], stack);+ });+ stack.pop();+ return sortedObj;+ } else {+ return obj;+ }+}+/*** Check that a / b have the same type.*diff --git a/lib/utils.js b/lib/utils.jsindex e21ddd9b..37fd5d7e 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -43,6 +43,22 @@ exports.forEach = function(arr, fn, scope){fn.call(scope, arr[i], i);};+/**+ * Array#map (<=IE8)+ *+ * @param {Array} array+ * @param {Function} fn+ * @param {Object} scope+ * @api private+ */++exports.map = function(arr, fn, scope){+ var result = [];+ for (var i = 0, l = arr.length; i < l; i++)+ result.push(fn.call(scope, arr[i], i));+ return result;+};+/*** Array#indexOf (<=IE8)*