Skip to content

Commit

Permalink
Revert jestjs#7089 and add tests to catch the regression in the future.
Browse files Browse the repository at this point in the history
  • Loading branch information
scotthovestadt committed May 1, 2019
1 parent a9c0e6b commit 573a80f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 95 deletions.
14 changes: 14 additions & 0 deletions e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap
Expand Up @@ -2,10 +2,24 @@

exports[`Snapshot serializers renders snapshot 1`] = `
Object {
"snapshot serializers works with array of strings in property matcher 1": "
Object {
\\"arrayOfStrings\\": Array [
\\"stream\\",
],
}
",
"snapshot serializers works with default serializers 1": "
<div
id=\\"foo\\"
/>
",
"snapshot serializers works with expect.XXX within array in property matcher 1": "
Object {
\\"arrayOfStrings\\": Array [
Any<String>,
],
}
",
"snapshot serializers works with first plugin 1": "foo - 1",
"snapshot serializers works with nested serializable objects 1": "foo - bar - 2",
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/snapshotSerializers.test.ts
Expand Up @@ -20,8 +20,8 @@ const runAndAssert = () => {
'--no-cache',
]);
const json = result.json;
expect(json.numTotalTests).toBe(7);
expect(json.numPassedTests).toBe(7);
expect(json.numTotalTests).toBe(9);
expect(json.numPassedTests).toBe(9);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(0);
expect(result.status).toBe(0);
Expand Down
16 changes: 16 additions & 0 deletions e2e/snapshot-serializers/__tests__/snapshot.test.js
Expand Up @@ -91,4 +91,20 @@ describe('snapshot serializers', () => {
});
expect(test).toMatchSnapshot();
});

it('works with array of strings in property matcher', () => {
expect({
arrayOfStrings: ['stream'],
}).toMatchSnapshot({
arrayOfStrings: ['stream'],
});
});

it('works with expect.XXX within array in property matcher', () => {
expect({
arrayOfStrings: ['stream'],
}).toMatchSnapshot({
arrayOfStrings: [expect.any(String)],
});
});
});
80 changes: 4 additions & 76 deletions packages/jest-snapshot/src/__tests__/utils.test.ts
Expand Up @@ -9,7 +9,6 @@ jest.mock('fs');

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

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

describe('DeepMerge', () => {
it('Correctly merges objects with property matchers', () => {
/* 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 target = {data: {bar: 'bar', foo: 'foo'}};

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

const mergedOutput = deepMerge(target, propertyMatchers);

// 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 */
expect(mergedOutput).toStrictEqual({data: {bar: 'bar', foo: matcher}});
expect(target).toStrictEqual({data: {bar: 'bar', foo: 'foo'}});
});
});
17 changes: 0 additions & 17 deletions packages/jest-snapshot/src/utils.ts
Expand Up @@ -178,30 +178,13 @@ 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 573a80f

Please sign in to comment.