Skip to content

Commit

Permalink
Merge pull request #543 from unexpectedjs/fix/satisfyDereference
Browse files Browse the repository at this point in the history
<object> to satisfy <object>: Do not dereference properties that aren't needed for the assertion
  • Loading branch information
papandreou committed Dec 26, 2018
2 parents 2910155 + 7dbe873 commit 0a3d6ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1827,26 +1827,23 @@ module.exports = expect => {
const promiseByKey = {};
let forceExhaustivelyComparison = false;
uniqueKeys.forEach((key, index) => {
const subjectKey = subjectType.hasKey(subject, key)
? subjectType.valueForKey(subject, key)
: undefined;
const subjectHasKey = subjectType.hasKey(subject, key);
const valueKey = valueType.hasKey(value, key, true)
? valueType.valueForKey(value, key)
: undefined;
const valueKeyType = expect.findTypeOf(valueKey);
const isDefinedSubjectKey = typeof subjectKey !== 'undefined';
const isDefinedValueKey = typeof valueKey !== 'undefined';
if (expect.flags.exhaustively) {
if (valueKeyType.is('expect.it') && !isDefinedSubjectKey) {
if (valueKeyType.is('expect.it') && !subjectHasKey) {
// ensure value only expect.it key is marked missing
forceExhaustivelyComparison = true;
}
} else {
if (isDefinedSubjectKey && !isDefinedValueKey) {
// ignore subject only keys unless we are being exhaustive
return;
}
} else if (subjectHasKey && typeof valueKey === 'undefined') {
// ignore subject only keys unless we are being exhaustive
return;
}
const subjectKey = subjectHasKey
? subjectType.valueForKey(subject, key)
: undefined;

promiseByKey[key] = expect.promise(() => {
if (valueKeyType.is('expect.it')) {
Expand Down
13 changes: 13 additions & 0 deletions test/assertions/to-satisfy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2564,4 +2564,17 @@ describe('to satisfy assertion', () => {
});
});
});

it('should not dereference properties that are not being asserted on', function() {
expect(
{
get ohNo() {
throw new Error('argh');
},
foo: 123
},
'to satisfy',
{ foo: 123 }
);
});
});

0 comments on commit 0a3d6ff

Please sign in to comment.