Skip to content

Commit

Permalink
Fix bug when asserting some valid ES6 keys
Browse files Browse the repository at this point in the history
Resolution of chaijs#674
  • Loading branch information
meeber committed Apr 8, 2016
1 parent 6b640f7 commit aa1bcb4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,11 @@ module.exports = function (chai, _) {
} else {
actual = Object.keys(obj);

if (typeof Object.getOwnPropertySymbols === 'function') {
// Object.keys excludes Symbols so we need this too
actual = actual.concat(Object.getOwnPropertySymbols(obj));
}

switch (_.type(keys)) {
case 'array':
if (arguments.length > 1) throw new Error(mixedArgsMsg);
Expand All @@ -1184,7 +1189,10 @@ module.exports = function (chai, _) {
keys = Array.prototype.slice.call(arguments);
}

keys = keys.map(String);
// Only stringify non-Symbols because Symbols would become "Symbol()"
keys = keys.map(function (val) {
return typeof val === 'symbol' ? val : String(val);
});
}

if (!keys.length) throw new Error('keys required');
Expand Down Expand Up @@ -1248,8 +1256,8 @@ module.exports = function (chai, _) {
ok
, 'expected #{this} to ' + str
, 'expected #{this} to not ' + str
, expected.slice(0).sort()
, actual.sort()
, expected
, actual
, true
);
}
Expand Down
49 changes: 49 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,15 @@ 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 });

if (typeof Symbol === 'function') {
var sym1 = Symbol()
, sym2 = 42
, obj = {};
obj[sym1] = 'val1';
obj[sym2] = 'val2';
expect(obj).to.have.all.keys([sym1, sym2]);
}

if (typeof Map !== 'undefined') {
var aKey = {thisIs: 'anExampleObject'};
var anotherKey = {doingThisBecauseOf: 'referential equality'};
Expand All @@ -766,6 +775,26 @@ describe('expect', function () {
expect(new Map([[aKey, 'aValue'], [anotherKey, 'anotherValue']])).to.not.have.any.keys([20, 1, {13: 37}]);
expect(new Map([[aKey, 'aValue'], [anotherKey, 'anotherValue']])).to.not.have.all.keys([aKey, {'iDoNot': 'exist'}]);

var weirdMapKey1 = Object.create(null)
, weirdMapKey2 = {toString: null};
expect(new Map([[weirdMapKey1, 'val1'], [weirdMapKey2, 'val2']])).to.have.all.keys([weirdMapKey1, weirdMapKey2]);

if (typeof Symbol === 'function') {
var symMapKey1 = Symbol()
, symMapKey2 = Symbol()
, symMapKey3 = Symbol();

expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.have.all.keys(symMapKey1, symMapKey2);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.have.any.keys(symMapKey1, symMapKey3);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.contain.all.keys(symMapKey2, symMapKey1);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.contain.any.keys(symMapKey3, symMapKey1);

expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.not.have.all.keys(symMapKey1, symMapKey3);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.not.have.any.keys(symMapKey3);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.not.contain.all.keys(symMapKey3, symMapKey1);
expect(new Map([[symMapKey1, 'symValue1'], [symMapKey2, 'symValue2']])).to.not.contain.any.keys(symMapKey3);
}

err(function(){
expect(new Map().set({ foo: 1 })).to.have.keys();
}, "keys required");
Expand Down Expand Up @@ -806,6 +835,26 @@ describe('expect', function () {
expect(new Set([aKey, anotherKey])).to.not.have.any.keys([20, 1, {13: 37}]);
expect(new Set([aKey, anotherKey])).to.not.have.all.keys([aKey, {'iDoNot': 'exist'}]);

var weirdSetKey1 = Object.create(null)
, weirdSetKey2 = {toString: null};
expect(new Map([[weirdSetKey1, 'val1'], [weirdSetKey2, 'val2']])).to.have.all.keys([weirdSetKey1, weirdSetKey2]);

if (typeof Symbol === 'function') {
var symSetKey1 = Symbol()
, symSetKey2 = Symbol()
, symSetKey3 = Symbol();

expect(new Set([symSetKey1, symSetKey2])).to.have.all.keys(symSetKey1, symSetKey2);
expect(new Set([symSetKey1, symSetKey2])).to.have.any.keys(symSetKey1, symSetKey3);
expect(new Set([symSetKey1, symSetKey2])).to.contain.all.keys(symSetKey2, symSetKey1);
expect(new Set([symSetKey1, symSetKey2])).to.contain.any.keys(symSetKey3, symSetKey1);

expect(new Set([symSetKey1, symSetKey2])).to.not.have.all.keys(symSetKey1, symSetKey3);
expect(new Set([symSetKey1, symSetKey2])).to.not.have.any.keys(symSetKey3);
expect(new Set([symSetKey1, symSetKey2])).to.not.contain.all.keys(symSetKey3, symSetKey1);
expect(new Set([symSetKey1, symSetKey2])).to.not.contain.any.keys(symSetKey3);
}

err(function(){
expect(new Set().add({ foo: 1 })).to.have.keys();
}, "keys required");
Expand Down

0 comments on commit aa1bcb4

Please sign in to comment.