Skip to content

Commit

Permalink
fix: correctly handle arrays in snapshot deep merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmydief committed Aug 13, 2020
1 parent a176c30 commit 5fcc6ec
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
44 changes: 44 additions & 0 deletions packages/jest-snapshot/src/__tests__/utils.test.ts
Expand Up @@ -419,6 +419,50 @@ 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
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 5fcc6ec

Please sign in to comment.