Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparing certain objects throws TypeError: Cannot convert object to primitive value #2109

Closed
joshlory opened this issue Feb 16, 2016 · 0 comments

Comments

@joshlory
Copy link

When comparing objects with certain length properties, the reporter throws when stringifying the diff. Reduced repro:

it('compare two objects', function () {
   assert.deepEqual({length: {toString: 0}}, {length: 1});
});

Expected:

AssertionError: { length: { toString: 0 } } deepEqual { length: 1 }
+ expected
- actual

 {
-  "length": {
-    "toString": 0
-  }
+  "length": 1
 }

Actual:

TypeError: Cannot convert object to primitive value
at jsonStringify (mocha/lib/utils.js:504:7)
at Object.exports.stringify (mocha/lib/utils.js:431:14)
at mocha/lib/reporters/base.js:203:37
at Array.forEach (native)
at Function.exports.list (mocha/lib/reporters/base.js:163:12)
at Spec.Base.epilogue (mocha/lib/reporters/base.js:328:10)
at Runner.emit (events.js:129:20)
at mocha/lib/runner.js:791:12
at mocha/lib/runner.js:644:9
at next (mocha/lib/runner.js:284:14)
joshlory pushed a commit to joshlory/mocha that referenced this issue Feb 16, 2016
…length

Root cause is that some JS objects cannot be coerced to a Number:

Number({toString: 0});
TypeError: can't convert ({toString:0}) to number
Number({a: 0});
NaN
danielstjules added a commit that referenced this issue Mar 20, 2016
Fix #2109: stringify should handle objects with non-coercible length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant