From 68f3c02f07a29afa3780006aa831062f7aded609 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 | 9 +++++++++ test/should.js | 9 +++++++++ 3 files changed, 19 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..acb02dd07 100644 --- a/test/expect.js +++ b/test/expect.js @@ -668,6 +668,15 @@ describe('expect', function () { }); + it('keys(array) will not mutate array (#359)', 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).deep.equal(original_order); + }); + it('chaining', function(){ var tea = { name: 'chai', extras: ['milk', 'sugar', 'smile'] }; expect(tea).to.have.property('extras').with.lengthOf(3); diff --git a/test/should.js b/test/should.js index c122a01a6..d28861f06 100644 --- a/test/should.js +++ b/test/should.js @@ -525,6 +525,15 @@ describe('should', function() { }, "expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'"); }); + it('keys(array) will not mutate array (#359)', 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); + }); + it('throw', function () { // See GH-45: some poorly-constructed custom errors don't have useful names // on either their constructor or their constructor prototype, but instead