Skip to content

Commit c9c8dba

Browse files
authored
fix: correctly handle arrays in snapshot deep merge (#10404)
1 parent a176c30 commit c9c8dba

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

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

910
### Chore & Maintenance
1011

packages/jest-snapshot/src/__tests__/utils.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,26 @@ describe('DeepMerge with property matchers', () => {
419419
},
420420
},
421421
],
422+
423+
[
424+
'an array of objects',
425+
// Target
426+
[{name: 'one'}, {name: 'two'}, {name: 'three'}],
427+
// Matchers
428+
[{name: 'one'}, {name: matcher}, {name: matcher}],
429+
// Expected
430+
[{name: 'one'}, {name: matcher}, {name: matcher}],
431+
],
432+
433+
[
434+
'an array of arrays',
435+
// Target
436+
[['one'], ['two'], ['three']],
437+
// Matchers
438+
[['one'], [matcher], [matcher]],
439+
// Expected
440+
[['one'], [matcher], [matcher]],
441+
],
422442
];
423443
/* eslint-enable sort-keys */
424444

packages/jest-snapshot/src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ const deepMergeArray = (target: Array<any>, source: Array<any>) => {
231231

232232
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
233233
export const deepMerge = (target: any, source: any): any => {
234-
const mergedOutput = {...target};
235234
if (isObject(target) && isObject(source)) {
235+
const mergedOutput = {...target};
236+
236237
Object.keys(source).forEach(key => {
237238
if (isObject(source[key]) && !source[key].$$typeof) {
238239
if (!(key in target)) Object.assign(mergedOutput, {[key]: source[key]});
@@ -243,6 +244,11 @@ export const deepMerge = (target: any, source: any): any => {
243244
Object.assign(mergedOutput, {[key]: source[key]});
244245
}
245246
});
247+
248+
return mergedOutput;
249+
} else if (Array.isArray(target) && Array.isArray(source)) {
250+
return deepMergeArray(target, source);
246251
}
247-
return mergedOutput;
252+
253+
return target;
248254
};

0 commit comments

Comments
 (0)