From 573a80fe8d797d0f9812472334a3e01f26ffa0a1 Mon Sep 17 00:00:00 2001 From: Scott Hovestadt Date: Wed, 1 May 2019 12:18:30 -0700 Subject: [PATCH] Revert #7089 and add tests to catch the regression in the future. --- .../snapshotSerializers.test.ts.snap | 14 ++++ e2e/__tests__/snapshotSerializers.test.ts | 4 +- .../__tests__/snapshot.test.js | 16 ++++ .../jest-snapshot/src/__tests__/utils.test.ts | 80 +------------------ packages/jest-snapshot/src/utils.ts | 17 ---- 5 files changed, 36 insertions(+), 95 deletions(-) diff --git a/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap b/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap index 4acc30034214..cc4cb7ebee4b 100644 --- a/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap +++ b/e2e/__tests__/__snapshots__/snapshotSerializers.test.ts.snap @@ -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": "
+", + "snapshot serializers works with expect.XXX within array in property matcher 1": " +Object { + \\"arrayOfStrings\\": Array [ + Any, + ], +} ", "snapshot serializers works with first plugin 1": "foo - 1", "snapshot serializers works with nested serializable objects 1": "foo - bar - 2", diff --git a/e2e/__tests__/snapshotSerializers.test.ts b/e2e/__tests__/snapshotSerializers.test.ts index 00748140f7ed..210dada06e02 100644 --- a/e2e/__tests__/snapshotSerializers.test.ts +++ b/e2e/__tests__/snapshotSerializers.test.ts @@ -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); diff --git a/e2e/snapshot-serializers/__tests__/snapshot.test.js b/e2e/snapshot-serializers/__tests__/snapshot.test.js index fb76bb95a948..093024192c62 100644 --- a/e2e/snapshot-serializers/__tests__/snapshot.test.js +++ b/e2e/snapshot-serializers/__tests__/snapshot.test.js @@ -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)], + }); + }); }); diff --git a/packages/jest-snapshot/src/__tests__/utils.test.ts b/packages/jest-snapshot/src/__tests__/utils.test.ts index f28efd12128d..93193ff5977c 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.ts +++ b/packages/jest-snapshot/src/__tests__/utils.test.ts @@ -9,7 +9,6 @@ jest.mock('fs'); import fs from 'fs'; import path from 'path'; -import assert from 'assert'; import chalk from 'chalk'; import { @@ -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'}}); }); }); diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index d0e4b20eb049..992fa72926d0 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -178,21 +178,6 @@ export const saveSnapshotFile = ( ); }; -const deepMergeArray = (target: Array, source: Array) => { - // 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)) { @@ -200,8 +185,6 @@ export const deepMerge = (target: any, source: any) => { 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]}); }