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

Unexpected behavior: keys() mutates the inputted array #376

Closed
jwngr opened this issue Feb 23, 2015 · 2 comments
Closed

Unexpected behavior: keys() mutates the inputted array #376

jwngr opened this issue Feb 23, 2015 · 2 comments

Comments

@jwngr
Copy link

jwngr commented Feb 23, 2015

I ran into an unexpected behavior with chai that I think should be considered a bug. Here is a reduced repro of the issue which currently fails:

it('keys() should not mutate inputted array', function() {
  var expectedKeys = ['b', 'a'];
  expect({a: true, b: true}).to.have.keys(expectedKeys);
  expect(expectedKeys[0]).to.equal('b');
});

Calling keys() should be able to do the comparison without mutating the inputted array since that array could be used in later assertions.

@keithamus
Copy link
Member

Hey @jwngr thanks for the issue!

#359 addressed a very similar bug, except it was with the array given as the expect param. It was quite a simple fix - in this case, we just need to change assertions.jsL1084 from actual.sort() to actual.slice(0).sort(). Of course then you'll also need to write some tests to back it up (you can copy the existing tests from #359 here and here and just tweak them to assert on the keys() param instead).

If you'd like to write up a PR for this, then please do so, and I'll happily merge it in. Doing so means you'll get on our hall of fame 😄

@jwngr
Copy link
Author

jwngr commented Feb 24, 2015

It looks like this is actually just a dupe of #359. Upgrading to chai 2.x.x confirms as much. Glad it's already fixed!

Updating the line you suggested won't have any effect since actual is created from an Object.keys() call. So the sort() there isn't necessary.

Thanks for the quick turnaround!

@jwngr jwngr closed this as completed Feb 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants