question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ES6] should.equal fails for Symbols

See original GitHub issue

Hallo 😄

I ran into this small issue while testing Symbol equality using the should syntax:

let prince = Symbol("Love");

expect(prince).to.equal(prince);  // Passes
prince.should.equal(prince);  // AssertionError: expected {} to equal {}

Adding || this instanceof Symbol to the shouldGetter resolved the issue. I’m not sure if a PR is desired since it’s ES6-specific?

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
meebercommented, Apr 7, 2016

Sounds good 😄

I’ll submit a PR after work today if it’s still open then.

Edit: Working on this now.

0reactions
keithamuscommented, Apr 7, 2016

I guess the main question is: If a user does shadow an object’s valueOf with a function, is there any potential for an assertion to behave differently than before since this.valueOf() would now be passed in instead of this?

Yeah… you raise a good point here. This could be potentially dangerous for that exact reason actually. Probably best just to add the extra Symbol code there, as per your original comment. In fact we could handle it a bit easier by doing typeof this === 'symbol' - seeing as every browser that supports Symbols also supports typeof Symbol() === 'symbol'.

Regardless, probably good to add a pair of Symbol tests…

Absolutely.

I’ll mark this as PR wanted - based on the following criteria:

  • The PR just adds the extra check for Symbols in the if - as described above
  • The PR has tests to back up the correct behaviour for should
  • Those tests should be conditional, so the tests still pass in browsers which don’t support Symbols.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Equality (==) - JavaScript - MDN Web Docs
The equality ( == ) operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, ...
Read more >
What is the motivation for bringing Symbols to ES6?
The original motivation for introducing symbols to Javascript was to enable private properties. Unfortunately, they ended up being severely ...
Read more >
Metaprogramming in ES6: Symbols and why they're awesome
Symbols will never conflict with Object string keys. · Symbols cannot be read using existing reflection tools. · Symbols are not private.
Read more >
Understanding symbols in JavaScript - LogRocket Blog
Starting with ES6, symbols were added to the primitives group. ... using the factory function Symbol() , their values will not be equal....
Read more >
A practical guide to ES6 Symbol - codeburst
While the example above could be achieved without Symbols (either as its own method or by overwriting String.prototype ), there are well-known ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found