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
9 changes: 7 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2551,13 +2551,18 @@ function assertKeys(keys) {

// Has all
if (all) {
const contains = flag(this, 'contains');
const remaining = actual.slice();
ok = uniqueExpected.every(function (expectedKey) {
return actual.some(function (actualKey) {
const matchIndex = remaining.findIndex(function (actualKey) {
return isEql(expectedKey, actualKey);
});
if (matchIndex === -1) return false;
if (!contains) remaining.splice(matchIndex, 1);
return true;
});

if (!flag(this, 'contains')) {
if (!contains) {
ok = ok && uniqueExpected.length == actual.length;
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,26 @@ describe('expect', function () {
}, "blah: expected { a: { b: 2 } } to not have deep own property 'a' of { b: 2 }");
});

['Map', 'Set'].forEach(function (kind) {
it('matches each actual entry once for all deep ' + kind + ' keys', function () {
var actual = kind === 'Map'
? new Map([[{id: 1}, 'one'], [{id: 2}, 'two']])
: new Set([{id: 1}, {id: 2}]);
var repeated = [{id: 1}, {id: 1}];
err(function () {
expect(actual).to.have.all.deep.keys(repeated);
});
expect(actual).to.not.have.all.deep.keys(repeated);
expect(actual).to.contain.all.deep.keys(repeated);
expect(actual).to.have.any.deep.keys(repeated);

var matching = kind === 'Map'
? new Map([[{id: 1}, 'one'], [{id: 1}, 'two']])
: new Set([{id: 1}, {id: 1}]);
expect(matching).to.have.all.deep.keys(repeated);
});
});

it('keys(array|Object|arguments)', function(){
expect({ foo: 1 }).to.have.keys(['foo']);
expect({ foo: 1 }).have.keys({ 'foo': 6 });
Expand Down