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

Revert #7089 and add tests to catch the regression(s) in the future. #8405

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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