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

<object> to satisfy <object>: Do not dereference properties that aren't needed for the assertion #543

Merged
merged 1 commit into from
Dec 26, 2018
Merged
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
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 }
);
});
});