From f37d2c223b5d2cd8ccbb054208d6294060a6900d Mon Sep 17 00:00:00 2001 From: Grant Snodgrass Date: Mon, 30 Jan 2017 18:22:51 -0500 Subject: [PATCH] fix: make negated `.keys` consider size of sets When neither the `contains` nor `any` flags are set, the `.keys` assertion implicitly means `.all.keys` and thus requires the target and given sets to be the same size. Therefore, `not.keys` implicitly means `.not.all.keys`, and should thus pass when the target and given sets aren't the same size, even if the target set is a superset of the given set. This commit enables this behavior. --- lib/chai/core/assertions.js | 2 +- test/assert.js | 1 + test/expect.js | 1 + test/should.js | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 2daf10bac..41b647ccf 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -1461,7 +1461,7 @@ module.exports = function (chai, _) { }); }); - if (!flag(this, 'negate') && !flag(this, 'contains')) { + if (!flag(this, 'contains')) { ok = ok && keys.length == actual.length; } } diff --git a/test/assert.js b/test/assert.js index d8d3db01a..f2fbd3de7 100644 --- a/test/assert.js +++ b/test/assert.js @@ -666,6 +666,7 @@ describe('assert', function () { assert.containsAllKeys({ foo: 1, bar: 2 }, { 'bar': 7, 'foo': 6 }); assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'baz' ]); + assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'foo' ]); assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'foo', 'baz' ]); assert.doesNotHaveAllKeys({ foo: 1, bar: 2, baz: 3 }, [ 'foo', 'bar', 'baz', 'fake' ]); assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'baz', 'foo' ]); diff --git a/test/expect.js b/test/expect.js index 1fecf02b7..13cc9a44e 100644 --- a/test/expect.js +++ b/test/expect.js @@ -1568,6 +1568,7 @@ describe('expect', function () { expect({ foo: 1, bar: 2, baz: 3 }).to.contain.all.keys(['bar', 'foo']); expect({ foo: 1, bar: 2 }).to.not.have.keys('baz'); + expect({ foo: 1, bar: 2 }).to.not.have.keys('foo'); expect({ foo: 1, bar: 2 }).to.not.have.keys('foo', 'baz'); expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz'); expect({ foo: 1, bar: 2 }).to.not.contain.keys('foo', 'baz'); diff --git a/test/should.js b/test/should.js index 612f74e90..cbb3902a4 100644 --- a/test/should.js +++ b/test/should.js @@ -1369,6 +1369,7 @@ describe('should', function() { ({ foo: 1, bar: 2 }).should.contain.keys({ 'foo': 6 }); ({ foo: 1, bar: 2 }).should.not.have.keys('baz'); + ({ foo: 1, bar: 2 }).should.not.have.keys('foo'); ({ foo: 1, bar: 2 }).should.not.have.keys('foo', 'baz'); ({ foo: 1, bar: 2 }).should.not.contain.keys('baz'); ({ foo: 1, bar: 2 }).should.not.contain.keys('foo', 'baz');