Skip to content

Commit

Permalink
fix: correctly handle arrays in snapshot deep merge (#10404)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmydief authored Aug 14, 2020
1 parent a176c30 commit c9c8dba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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;
};

0 comments on commit c9c8dba

Please sign in to comment.