From d26d39fffbc606fa65eb1fbbaa96a1a357e1dbb2 Mon Sep 17 00:00:00 2001 From: Gregg Lind Date: Wed, 4 Feb 2015 16:48:16 -0600 Subject: [PATCH] Fix #359 keys() sorts input unexpectedly --- lib/chai/core/assertions.js | 2 +- test/expect.js | 12 ++++++++++++ test/should.js | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 349e53ddc..4a4d2d28f 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -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 ); diff --git a/test/expect.js b/test/expect.js index bd5dcc7ec..f6d344523 100644 --- a/test/expect.js +++ b/test/expect.js @@ -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"); diff --git a/test/should.js b/test/should.js index c122a01a6..69d02596a 100644 --- a/test/should.js +++ b/test/should.js @@ -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");