Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept empty keys when actual object is empty #1384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/chai/core/assertions.js
Expand Up @@ -2386,7 +2386,7 @@ module.exports = function (chai, _) {
});
}

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

Expand Down
10 changes: 10 additions & 0 deletions test/assert.js
Expand Up @@ -994,6 +994,9 @@ describe('assert', function () {
assert.hasAllKeys({ foo: 1, bar: 2 }, [ 'foo', 'bar' ]);
assert.hasAllKeys({ foo: 1 }, { foo: 30 });
assert.hasAllKeys({ foo: 1, bar: 2 }, { 'foo': 6, 'bar': 7 });
assert.hasAllKeys({});
assert.hasAllKeys({}, []);
assert.hasAllKeys({}, {});

assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, [ 'foo', 'bar' ]);
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, [ 'bar', 'foo' ]);
Expand All @@ -1004,6 +1007,9 @@ describe('assert', function () {
assert.containsAllKeys({ foo: 1, bar: 2 }, { 'bar': 7 });
assert.containsAllKeys({ foo: 1, bar: 2 }, { 'foo': 6 });
assert.containsAllKeys({ foo: 1, bar: 2 }, { 'bar': 7, 'foo': 6 });
assert.containsAllKeys({});
assert.containsAllKeys({}, []);
assert.containsAllKeys({}, {});

assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'baz' ]);
assert.doesNotHaveAllKeys({ foo: 1, bar: 2 }, [ 'foo' ]);
Expand All @@ -1028,6 +1034,10 @@ describe('assert', function () {
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2 }, { baz: 1, biz: 2, fake: 3 });
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2 }, { baz: 1 });

assert.doesNotHaveAnyKeys({});
assert.doesNotHaveAnyKeys({}, []);
assert.doesNotHaveAnyKeys({}, {});

var enumProp1 = 'enumProp1'
, enumProp2 = 'enumProp2'
, nonEnumProp = 'nonEnumProp'
Expand Down
11 changes: 11 additions & 0 deletions test/expect.js
Expand Up @@ -2566,6 +2566,17 @@ describe('expect', function () {
expect({ foo: 1, bar: 2 }).not.have.all.keys({ 'baz': 8, 'foo': 7 });
expect({ foo: 1, bar: 2 }).not.contain.all.keys({ 'baz': 8, 'foo': 7 });

expect({}).to.have.all.keys();
expect({}).to.have.all.keys({});
expect({}).to.have.all.keys([]);
expect({}).contain.keys();
expect({}).contain.keys({});
expect({}).contain.keys([]);

expect({}).to.not.have.any.keys();
expect({}).not.have.any.keys({});
expect({}).not.any.keys([]);

var enumProp1 = 'enumProp1'
, enumProp2 = 'enumProp2'
, nonEnumProp = 'nonEnumProp'
Expand Down
11 changes: 11 additions & 0 deletions test/should.js
Expand Up @@ -2165,6 +2165,17 @@ describe('should', function() {
({ 1: 1, 2: 2 }).should.have.any.keys(1, 3);
({ 1: 1, 2: 2 }).should.contain.keys(1);

({}).should.have.all.keys();
({}).should.have.all.keys([]);
({}).should.have.all.keys({});
({}).should.contain.keys();
({}).should.contain.keys([]);
({}).should.contain.keys({});

({}).should.not.have.any.keys();
({}).should.not.have.any.keys([]);
({}).should.not.have.any.keys({});

var enumProp1 = 'enumProp1'
, enumProp2 = 'enumProp2'
, nonEnumProp = 'nonEnumProp'
Expand Down