deep-eql icon indicating copy to clipboard operation
deep-eql copied to clipboard

URLSearchParams deep equal not working

Open Elias-Graf opened this issue 4 years ago • 2 comments

I'm trying to test if my URLSearchParams are correct with the to.deep.equal option but it returns true (is equal) when it shouldn't.

See the running code at https://stackblitz.com/edit/node-qx7qra?file=index.test.js

Let's assume that we're testing this function:

function getIncorrectParams() {
  return new URLSearchParams({
    from: '2020',
    to: '2021',
    ids: ['abc-1'],
  });
}

If I write:

test('via string', () => {
  expect(getIncorrectParams().toString()).not.to.equal(
    new URLSearchParams({
      from: '2020',
      to: '2021',
      ids: ['abc-1', 'abc-2'],
    }).toString()
  );
});

It correctly notices that the strings are not the same, good. But as soon as I remove the toString and test with:

test('via deep equal', () => {
  expect(getIncorrectParams()).not.to.deep.equal(
    new URLSearchParams({
      from: '2020',
      to: '2021',
      ids: ['abc-1', 'abc-2'],
    })
  );
});

It claims that the assertion failed because the two objects are "equal". Note if you actually go ahead and run the example, the debug output clearly shows that there are differences!

And the deep.equal property works if I use "normal" object. See this example where I convert the URLSearchParams to a "plain object" first:

test('deep equal plain objects', () => {
  expect(Object.fromEntries(getIncorrectParams().entries())).not.to.deep.equal({
    from: '2020',
    to: '2021',
    ids: ['abc-1', 'abc-2'],
  });
});

Elias-Graf avatar Oct 01 '21 09:10 Elias-Graf

Hey @Elias-Graf; this is probably an issue with https://github.com/chaijs/deep-eql. I think it doesn't account for URLSearchParams. Investigating the issue in chai and then making a PR to fix the issue either in chai or deep-eql is your best bet. We are open to pull requests and are really happy to have new contributors to chai.

koddsson avatar Oct 04 '21 20:10 koddsson

@koddsson Hi! I see if I can take some time to dive into this. Most of the time it's relatively difficult to dive into an existing project. The thing is that as far as I know, everything in JavaScript is an object. So I always assumed everything could be compared in the same way. Note that I'm talking about equality, not comparing pointers.

Also, can I like move this issue to the correct repository? Or is that not a thing?.

Elias-Graf avatar Oct 05 '21 09:10 Elias-Graf