Skip to content

Commit

Permalink
Support symbols in <object> [not] to [only] have [own] properties
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Feb 9, 2020
1 parent 7d9963d commit 659461f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
21 changes: 11 additions & 10 deletions lib/assertions.js
Expand Up @@ -320,20 +320,15 @@ module.exports = expect => {
expect.addAssertion(
'<object> [not] to [only] have [own] properties <array>',
(expect, subject, propertyNames) => {
const unsupportedPropertyNames = [];
propertyNames.forEach(propertyName => {
if (
typeof propertyName !== 'string' &&
typeof propertyName !== 'number'
) {
unsupportedPropertyNames.push(propertyName);
}
const unsupportedPropertyNames = propertyNames.filter(propertyName => {
const type = typeof propertyName;
return type !== 'string' && type !== 'number' && type !== 'symbol';
});
if (unsupportedPropertyNames.length > 0) {
expect.errorMode = 'nested';
expect.fail(function() {
this.error(
'All expected properties must be passed as strings or numbers, but these are not:'
'All expected properties must be passed as strings, symbols, or numbers, but these are not:'
).indentLines();
unsupportedPropertyNames.forEach(function(propertyName) {
this.nl()
Expand Down Expand Up @@ -443,7 +438,13 @@ module.exports = expect => {
);
} else {
propertyNames.forEach(propertyName => {
expect(subject, '[not] to have [own] property', String(propertyName));
expect(
subject,
'[not] to have [own] property',
typeof propertyName === 'number'
? String(propertyName)
: propertyName
);
});
}
}
Expand Down
45 changes: 42 additions & 3 deletions test/assertions/to-have-properties.spec.js
Expand Up @@ -252,7 +252,7 @@ describe('to have properties assertion', () => {
},
'to error',
'expected { true: 123 } to have properties [ true ]\n' +
' All expected properties must be passed as strings or numbers, but these are not:\n' +
' All expected properties must be passed as strings, symbols, or numbers, but these are not:\n' +
' true'
);
});
Expand All @@ -271,7 +271,7 @@ describe('to have properties assertion', () => {
'to error',
'expected { foo: 123 }\n' +
"to have properties [ { toString: function toString() { return 'foo'; } } ]\n" +
' All expected properties must be passed as strings or numbers, but these are not:\n' +
' All expected properties must be passed as strings, symbols, or numbers, but these are not:\n' +
" { toString: function toString() { return 'foo'; } }"
);
});
Expand All @@ -283,7 +283,7 @@ describe('to have properties assertion', () => {
},
'to error',
'expected { foo: 123 } to have properties [ true, false ]\n' +
' All expected properties must be passed as strings or numbers, but these are not:\n' +
' All expected properties must be passed as strings, symbols, or numbers, but these are not:\n' +
' true\n' +
' false'
);
Expand Down Expand Up @@ -392,4 +392,43 @@ describe('to have properties assertion', () => {
);
});
});

if (typeof Symbol === 'function') {
describe('with symbols', function() {
it('should pass if the object has the given symbol(s)', () => {
const symbol = Symbol('foo');
expect({ [symbol]: 123 }, 'to have properties', [symbol]);
});

it('should fail if the object is missing the given symbol(s)', () => {
const symbolFoo = Symbol('foo');
const symbolBar = Symbol('bar');
expect(
() => expect({ [symbolFoo]: 123 }, 'to have properties', [symbolBar]),
'to throw',
"expected { [Symbol('foo')]: 123 } to have properties [ Symbol('bar') ]"
);
});

describe('and the own flag', function() {
it('should pass if the object has the given own symbol(s)', () => {
const symbol = Symbol('foo');
expect({ [symbol]: 123 }, 'to have own properties', [symbol]);
});

it('should fail if the object is missing the given symbol(s)', () => {
const symbolFoo = Symbol('foo');
const symbolBar = Symbol('bar');
expect(
() =>
expect({ [symbolFoo]: 123 }, 'to have own properties', [
symbolBar
]),
'to throw',
"expected { [Symbol('foo')]: 123 } to have own properties [ Symbol('bar') ]"
);
});
});
});
}
});

0 comments on commit 659461f

Please sign in to comment.