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.

Deep equal with symbols as keys does not fail

See original GitHub issue

Hi,

The following test should fail but doesn’t:

let test = Symbol('test')
expect({[test]: 1}).to.deep.equal({[test]: 2})

Thanks! Florent

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
janlukasschroedercommented, Jul 25, 2019

There is a simple workaround: Node.js util.inspect

Example:

const { inspect } = require('util');
const { expect } = require('chai');

const foo = { [Symbol('a')]: ['1'] }; // also works with Sequelize operator, eg [Op.in]
const bar = { [Symbol('a')]: ['1'] };

const fooSerialized = inspect(foo, { depth: null });
const barSerialized = inspect(bar, { depth: null });

expect(fooSerialized).to.deep.equal(barSerialized);
4reactions
nemisjcommented, Oct 4, 2017

@shvaikalesh Hi. I have one real-world use case for symbols. Sequelize module recently implemented their operators using Symbols, since it’s more secure ( http://docs.sequelizejs.com/manual/tutorial/querying.html#operators ). Now, it’s much more tedious to write unit tests, in order to test these operators. It’s still possible, but quite challenging.

Check this. this is how it could look if chai would assert Symbols correctly:

expect(result).to.be.deep.equal({
  'name': {
    [Op.and]: [
      {
        [Op.or]: [
          { [Op.notIn]: [ 'lolipop', 'zork' ] },
          { [Op.eq]: null }
        ]
      },
      { [Op.in]: [ 'good', 'very-good' ] },
    ]
  }
});

( All the values inside Op are symbols.)

But, now, in order to test this, i have to do some totally crazy stuff.

Read more comments on GitHub >

github_iconTop Results From Across the Web

object - Javascript - deepEqual Comparison - Stack Overflow
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the...
Read more >
Expect / Should - Chai Assertion Library
Target object deeply (but not strictly) equals `{a: 1}` expect({a: 1}).to.deep.equal({a: ... Properties with Symbol-based keys are excluded from the count.
Read more >
Equality comparisons and sameness - JavaScript | MDN
If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have...
Read more >
But Why?. Symbols, the newest JavaScript… | by ... - Medium
In this case a symbols is similar to an object as creating multiple instances will result in values which are not exactly equal....
Read more >
Assert | Node.js v19.3.0 Documentation
If the values are not equal, an AssertionError is thrown with a message property set equal to the value of the message parameter....
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