Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2524,21 +2524,23 @@ function assertKeys(keys) {
});
}

if (!keys.length) {
throw new AssertionError(flagMsg + 'keys required', undefined, ssfi);
}

let len = keys.length,
any = flag(this, 'any'),
all = flag(this, 'all'),
expected = keys,
isEql = isDeep ? flag(this, 'eql') : (val1, val2) => val1 === val2;
isEql = isDeep ? flag(this, 'eql') : (val1, val2) => val1 === val2,
contains = flag(this, 'contains');

if (!any && !all) {
all = true;
}

const uniqueExpected = [...new Set(expected)];
const canHaveEmptyKeys = all && !contains && keysType !== 'undefined';

if (!keys.length && !canHaveEmptyKeys) {
throw new AssertionError(flagMsg + 'keys required', undefined, ssfi);
}

// Has any
if (any) {
Expand All @@ -2557,7 +2559,7 @@ function assertKeys(keys) {
});
});

if (!flag(this, 'contains')) {
if (!contains) {
ok = ok && uniqueExpected.length == actual.length;
}
}
Expand Down
28 changes: 4 additions & 24 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,18 +1195,10 @@ describe('assert', function () {

errMap.set({1: 20}, 'number');

err(function(){
assert.hasAllKeys(errMap, [], 'blah');
}, "blah: keys required");

err(function(){
assert.containsAllKeys(errMap, [], 'blah');
}, "blah: keys required");

err(function(){
assert.doesNotHaveAllKeys(errMap, [], 'blah');
}, "blah: keys required");

err(function(){
assert.hasAnyKeys(errMap, [], 'blah');
}, "blah: keys required");
Expand Down Expand Up @@ -1310,18 +1302,10 @@ describe('assert', function () {
errSet.add({1: 20});
errSet.add('number');

err(function(){
assert.hasAllKeys(errSet, [], 'blah');
}, "blah: keys required");

err(function(){
assert.containsAllKeys(errSet, [], 'blah');
}, "blah: keys required");

err(function(){
assert.doesNotHaveAllKeys(errSet, [], 'blah');
}, "blah: keys required");

err(function(){
assert.hasAnyKeys(errSet, [], 'blah');
}, "blah: keys required");
Expand All @@ -1340,18 +1324,10 @@ describe('assert', function () {
// assert.containsAllDeepKeys(new Set([{foo: 1}]), { iDoNotExist: 0 })
// }, 'expected [ { foo: 1 } ] to deeply contain key { iDoNotExist: 0 }');

err(function(){
assert.hasAllKeys({ foo: 1 }, [], 'blah');
}, "blah: keys required");

err(function(){
assert.containsAllKeys({ foo: 1 }, [], 'blah');
}, "blah: keys required");

err(function(){
assert.doesNotHaveAllKeys({ foo: 1 }, [], 'blah');
}, "blah: keys required");

err(function(){
assert.hasAnyKeys({ foo: 1 }, [], 'blah');
}, "blah: keys required");
Expand Down Expand Up @@ -1438,6 +1414,10 @@ describe('assert', function () {
}, "blah: expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'");
});

it('keys() with empty keys and non-contains', () => {
assert.hasAllKeys({}, []);
});

it('lengthOf', function() {
assert.lengthOf([1,2,3], 3);
assert.lengthOf('foobar', 6);
Expand Down
33 changes: 17 additions & 16 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2750,10 +2750,6 @@ describe('expect', function () {
expect(errMap, 'blah').to.have.keys();
}, "blah: keys required");

err(function(){
expect(errMap).to.have.keys([]);
}, "keys required");

err(function(){
expect(errMap).to.contain.keys();
}, "keys required");
Expand Down Expand Up @@ -2876,10 +2872,6 @@ describe('expect', function () {
expect(errSet, 'blah').to.have.keys();
}, "blah: keys required");

err(function(){
expect(errSet).to.have.keys([]);
}, "keys required");

err(function(){
expect(errSet).to.contain.keys();
}, "keys required");
Expand All @@ -2902,14 +2894,6 @@ describe('expect', function () {
expect({ foo: 1 }, 'blah').to.have.keys();
}, "blah: keys required");

err(function(){
expect({ foo: 1 }).to.have.keys([]);
}, "keys required");

err(function(){
expect({ foo: 1 }).to.not.have.keys([]);
}, "keys required");

err(function(){
expect({ foo: 1 }).to.contain.keys([]);
}, "keys required");
Expand Down Expand Up @@ -3019,6 +3003,23 @@ describe('expect', function () {

});

it('should accept empty keys if all and not contains', function () {
expect({}).to.have.all.keys([]);
err(() => {
expect({ a: 1 }).to.have.all.keys([]);
});
});

it('should throw if empty keys with any or contains', function () {
err(() => {
expect({}).to.have.any.keys([]);
}, 'keys required');

err(() => {
expect({}).to.contain.keys([]);
}, 'keys required');
});

it('keys(array) will not mutate array (#359)', function () {
var expected = [ 'b', 'a' ];
var original_order = [ 'b', 'a' ];
Expand Down
20 changes: 4 additions & 16 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -2247,10 +2247,6 @@ describe('should', function() {
errMap.should.have.keys();
}, "keys required");

err(function(){
errMap.should.have.keys([]);
}, "keys required");

err(function(){
errMap.should.contain.keys();
}, "keys required");
Expand Down Expand Up @@ -2374,10 +2370,6 @@ describe('should', function() {
errSet.should.have.keys();
}, "keys required");

err(function(){
errSet.should.have.keys([]);
}, "keys required");

err(function(){
errSet.should.contain.keys();
}, "keys required");
Expand All @@ -2398,14 +2390,6 @@ describe('should', function() {
({ foo: 1 }).should.have.keys();
}, "keys required");

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

err(function(){
({ foo: 1 }).should.not.have.keys([]);
}, "keys required");

err(function(){
({ foo: 1 }).should.contain.keys([]);
}, "keys required");
Expand Down Expand Up @@ -2511,6 +2495,10 @@ describe('should', function() {

});

it('keys() with empty keys and non-contains', () => {
({}).should.have.keys([]);
});

it('keys(array) will not mutate array (#359)', function () {
var expected = [ 'b', 'a' ];
var original_order = [ 'b', 'a' ];
Expand Down
Loading