Skip to content

Commit

Permalink
Fix chaijs#359 keys() sorts input unexpectedly
Browse files Browse the repository at this point in the history
  • Loading branch information
gregglind committed Feb 5, 2015
1 parent dfd73ff commit d26d39f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ module.exports = function (chai, _) {
ok
, 'expected #{this} to ' + str
, 'expected #{this} to not ' + str
, expected.sort()
, expected.slice(0).sort()
, actual.sort()
, true
);
Expand Down
12 changes: 12 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,18 @@ describe('expect', function () {
expect({ foo: 1, bar: 2 }).to.not.have.all.keys(['baz', 'foo']);
expect({ foo: 1, bar: 2 }).to.not.contain.all.keys(['baz', 'foo']);


var keys_maintain_original_order_issue359 = function () {
var expected = [ 'b', 'a' ];
var original_order = [ 'b', 'a' ];
var obj = { "b": 1, "a": 1 };
expect(expected).deep.equal(original_order);
expect(obj).keys(original_order);
expect(expected, "sorting happened unexpectedly in keys()")
.deep.equal(original_order);
}
keys_maintain_original_order_issue359();

err(function(){
expect({ foo: 1 }).to.have.keys();
}, "keys required");
Expand Down
10 changes: 10 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@ describe('should', function() {
({ foo: 1, bar: 2 }).should.not.have.all.keys(['baz', 'foo']);
({ foo: 1, bar: 2 }).should.not.contain.all.keys(['baz', 'foo']);

var keys_maintain_original_order_issue359 = function () {
var expected = [ 'b', 'a' ];
var original_order = [ 'b', 'a' ];
var obj = { "b": 1, "a": 1 };
expected.should.deep.equal(original_order);
obj.should.keys(original_order);
expected.should.deep.equal(original_order);
}
keys_maintain_original_order_issue359();

err(function(){
({ foo: 1 }).should.have.keys();
}, "keys required");
Expand Down

0 comments on commit d26d39f

Please sign in to comment.