Skip to content

Commit

Permalink
Reapply jestjs#7089
Browse files Browse the repository at this point in the history
  • Loading branch information
timtrinidad committed May 1, 2019
1 parent 9081612 commit 1a3b280
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
80 changes: 76 additions & 4 deletions packages/jest-snapshot/src/__tests__/utils.test.ts
Expand Up @@ -9,6 +9,7 @@ jest.mock('fs');

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

import {
Expand Down Expand Up @@ -201,14 +202,85 @@ test('serialize handles \\r\\n', () => {

describe('DeepMerge', () => {
it('Correctly merges objects with property matchers', () => {
const target = {data: {bar: 'bar', foo: 'foo'}};
/* eslint-disable sort-keys */
// to keep keys in numerical order rather than alphabetical
const target = {
data: {
one: 'one',
two: 'two',
three: [
{
four: 'four',
five: 'five',
},
// Include an array element not present in the propertyMatchers
{
six: 'six',
seven: 'seven',
},
],
eight: [{nine: 'nine'}],
},
};

const matcher = expect.any(String);
const propertyMatchers = {data: {foo: matcher}};
const propertyMatchers = {
data: {
two: matcher,
three: [
{
four: matcher,
},
],
eight: [
{nine: matcher},
// Include an array element not present in the target
{ten: matcher},
],
},
};

const mergedOutput = deepMerge(target, propertyMatchers);

expect(mergedOutput).toStrictEqual({data: {bar: 'bar', foo: matcher}});
expect(target).toStrictEqual({data: {bar: 'bar', foo: 'foo'}});
// Use assert.deepStrictEqual() instead of expect().toStrictEqual()
// since we want to actually validate that we got the matcher
// rather than treat it specially the way that expect() does
assert.deepStrictEqual(mergedOutput, {
data: {
one: 'one',
two: matcher,
three: [
{
four: matcher,
five: 'five',
},
{
six: 'six',
seven: 'seven',
},
],
eight: [{nine: matcher}, {ten: matcher}],
},
});

// Ensure original target is not modified
expect(target).toStrictEqual({
data: {
one: 'one',
two: 'two',
three: [
{
four: 'four',
five: 'five',
},
{
six: 'six',
seven: 'seven',
},
],
eight: [{nine: 'nine'}],
},
});
/* eslint-enable sort-keys */
});
});
17 changes: 17 additions & 0 deletions packages/jest-snapshot/src/utils.ts
Expand Up @@ -178,13 +178,30 @@ export const saveSnapshotFile = (
);
};

const deepMergeArray = (target: Array<any>, source: Array<any>) => {
// Clone target
const mergedOutput = target.slice();

source.forEach((element, index) => {
if (typeof mergedOutput[index] === 'undefined') {
mergedOutput[index] = element;
} else {
mergedOutput[index] = deepMerge(target[index], element);
}
});

return mergedOutput;
};

export const deepMerge = (target: any, source: any) => {
const mergedOutput = {...target};
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key]) && !source[key].$$typeof) {
if (!(key in target)) Object.assign(mergedOutput, {[key]: source[key]});
else mergedOutput[key] = deepMerge(target[key], source[key]);
} else if (Array.isArray(source[key])) {
mergedOutput[key] = deepMergeArray(target[key], source[key]);
} else {
Object.assign(mergedOutput, {[key]: source[key]});
}
Expand Down

0 comments on commit 1a3b280

Please sign in to comment.