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

Handle arrays when merging snapshots #7089

Merged
merged 10 commits into from
Apr 22, 2019
10 changes: 9 additions & 1 deletion packages/jest-snapshot/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jest.mock('fs');

import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import assert from 'assert';
import chalk from 'chalk';

import {
deepMerge,
Expand Down Expand Up @@ -219,6 +219,7 @@ describe('DeepMerge', () => {
seven: 'seven',
},
],
eight: [{nine: 'nine'}],
},
};

Expand All @@ -231,6 +232,11 @@ describe('DeepMerge', () => {
four: matcher,
},
],
eight: [
{nine: matcher},
// Include an array element not present in the target
{ten: matcher},
],
},
};

Expand All @@ -253,6 +259,7 @@ describe('DeepMerge', () => {
seven: 'seven',
},
],
eight: [{nine: matcher}, {ten: matcher}],
},
});

Expand All @@ -271,6 +278,7 @@ describe('DeepMerge', () => {
seven: 'seven',
},
],
eight: [{nine: 'nine'}],
},
});
/* eslint-enable sort-keys */
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ const deepMergeArray = (target: Array<any>, source: Array<any>) => {
source.forEach((element, index) => {
if (typeof mergedOutput[index] === 'undefined') {
mergedOutput[index] = element;
} else if (typeof element === 'undefined') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In adding tests to cover this line, I realized that this case really shouldn't happen (i.e. source having an array element that's undefined)

mergedOutput.push(target[index]);
} else {
mergedOutput[index] = deepMerge(target[index], element);
}
Expand Down