☕️ Classic, reliable, trusted test framework for Node.js and the browser
Fix #2109: stringify should handle objects with non-coercible length
Josh Lory3b92fcc03c52
diff --git a/lib/utils.js b/lib/utils.jsindex 7c3a8b66..427915ea 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -454,7 +454,7 @@ function jsonStringify(object, spaces, depth) {var space = spaces * depth;var str = isArray(object) ? '[' : '{';var end = isArray(object) ? ']' : '}';- var length = object.length || exports.keys(object).length;+ var length = typeof object.length === 'number' ? object.length : exports.keys(object).length;// `.repeat()` polyfillfunction repeat(s, n) {return new Array(n).join(s);diff --git a/test/acceptance/utils.js b/test/acceptance/utils.jsindex 5f67f274..c67fd9fc 100644--- a/test/acceptance/utils.js+++ b/test/acceptance/utils.js@@ -340,6 +340,10 @@ describe('lib/utils', function () {stringify({symbol: symbol}).should.equal('{\n "symbol": Symbol(value)\n}')});}++ it('should handle length properties that cannot be coerced to a number', function () {+ stringify({length: {toString: 0}}).should.equal('{\n "length": {\n "toString": 0\n }\n}');+ });});describe('type', function () {