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

fix: correctly handle arrays in snapshot deep merge #10404

Merged
merged 1 commit into from
Aug 14, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[jest-reporters]` Fixes notify reporter on Linux (using notify-send) ([#10393](https://github.com/facebook/jest/pull/10400))
- `[jest-snapshot]` Correctly handles arrays and property matchers in snapshots ([#10404](https://github.com/facebook/jest/pull/10404))

### Chore & Maintenance

Expand Down
20 changes: 20 additions & 0 deletions packages/jest-snapshot/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@ describe('DeepMerge with property matchers', () => {
},
},
],

[
'an array of objects',
// Target
[{name: 'one'}, {name: 'two'}, {name: 'three'}],
// Matchers
[{name: 'one'}, {name: matcher}, {name: matcher}],
// Expected
[{name: 'one'}, {name: matcher}, {name: matcher}],
],

[
'an array of arrays',
// Target
[['one'], ['two'], ['three']],
// Matchers
[['one'], [matcher], [matcher]],
// Expected
[['one'], [matcher], [matcher]],
],
];
/* eslint-enable sort-keys */

Expand Down
10 changes: 8 additions & 2 deletions packages/jest-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ const deepMergeArray = (target: Array<any>, source: Array<any>) => {

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const deepMerge = (target: any, source: any): any => {
const mergedOutput = {...target};
if (isObject(target) && isObject(source)) {
const mergedOutput = {...target};

Object.keys(source).forEach(key => {
if (isObject(source[key]) && !source[key].$$typeof) {
if (!(key in target)) Object.assign(mergedOutput, {[key]: source[key]});
Expand All @@ -243,6 +244,11 @@ export const deepMerge = (target: any, source: any): any => {
Object.assign(mergedOutput, {[key]: source[key]});
}
});

return mergedOutput;
} else if (Array.isArray(target) && Array.isArray(source)) {
return deepMergeArray(target, source);
}
return mergedOutput;

return target;
};