-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Correctly merge property matchers with the rest of the snapshot in toMatchSnapshot
.
#6528
Changes from 1 commit
a88a7c0
994e09e
cbf8f6b
7ac7f4d
a3b32ed
74796b0
d17e7f6
1b8dd70
deb016c
7032a35
e4fbf98
3902a22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,27 @@ const cleanup = (hasteFS: HasteFS, update: SnapshotUpdateState) => { | |
}; | ||
}; | ||
|
||
function isObject(item) { | ||
return (item && typeof item === 'object' && !Array.isArray(item)); | ||
} | ||
|
||
function deepMerge(target, source) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd very much prefer if we could find some deep merge on npm. If that's not possible, this should be moved to a util file and receive quite extensive unit tests 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a very good point. It doesn't make a lot of sense to clutter this file up with a general-purpose merge function. I'll take a look at this tonight. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue with all of the deep merge libs I tried is that they don't copy the prototype so they break the asymmetric matchers -- the strategy here is the solution I was going to go with as well |
||
let mergedOutput = Object.assign({}, target); | ||
if (isObject(target) && isObject(source)) { | ||
Object.keys(source).forEach(key => { | ||
if (isObject(source[key]) && !source[key].$$typeof) { | ||
if (!(key in target)) | ||
Object.assign(output, {[key]: source[key]}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where Object.assign(mergedOutput, {[key]: source[key]}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching that, it was late when I submitted this and I overlooked it. |
||
else | ||
output[key] = deepMerge(target[key], source[key]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} else { | ||
Object.assign(output, {[key]: source[key]}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
}); | ||
} | ||
return mergedOutput; | ||
} | ||
|
||
const toMatchSnapshot = function( | ||
received: any, | ||
propertyMatchers?: any, | ||
|
@@ -98,7 +119,7 @@ const toMatchSnapshot = function( | |
report, | ||
}; | ||
} else { | ||
Object.assign(received, propertyMatchers); | ||
received = deepMerge(received, propertyMatchers); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should refer to this PR, not the issue