From ff848f8d7f9a702ccee232791a1aef483e62389e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 17 Nov 2019 14:27:15 -0500 Subject: [PATCH 1/5] jest-snapshot: Omit irrelevant received properties --- .../__snapshots__/printSnapshot.test.ts.snap | 23 +++++++++------ .../src/__tests__/printSnapshot.test.ts | 28 ++++++++++++++++++- packages/jest-snapshot/src/printSnapshot.ts | 3 +- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap index b32622fee2b7..62ab03012dfb 100644 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap @@ -128,13 +128,11 @@ exports[`pass false toMatchInlineSnapshot with properties equals false with snap Snapshot name: \`with properties 1\` - Expected properties - 1 -+ Received value + 3 ++ Received value + 1 Object { - "id": "abcdef", + "id": "abcdefg", -+ "text": "Increase code coverage", -+ "type": "ADD_ITEM", } `; @@ -144,13 +142,11 @@ exports[`pass false toMatchInlineSnapshot with properties equals false without s Snapshot name: \`with properties 1\` - Expected properties - 1 -+ Received value + 3 ++ Received value + 1 Object { - "id": "abcdef", + "id": "abcdefg", -+ "text": "Increase code coverage", -+ "type": "ADD_ITEM", } `; @@ -211,13 +207,11 @@ exports[`pass false toMatchSnapshot with properties equals false isLineDiffable Snapshot name: \`with properties 1\` - Expected properties - 1 -+ Received value + 3 ++ Received value + 1 Object { - "id": "abcdef", + "id": "abcdefg", -+ "text": "Increase code coverage", -+ "type": "ADD_ITEM", } `; @@ -246,6 +240,17 @@ Snapshot: "inline snapshot" Received: "received" `; +exports[`printPropertiesAndReceived omit missing properties 1`] = ` +- Expected properties - 2 ++ Received value + 1 + + Object { +- "hash": Any, +- "path": Any, ++ "path": "…", + } +`; + exports[`printSnapshotAndReceived backtick single line expected and received 1`] = ` Snapshot: "var foo = \`backtick\`;" Received: "var foo = tag\`backtick\`;" diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index 885d030e2e6a..47d1c58b376c 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -11,7 +11,10 @@ import chalk = require('chalk'); import format = require('pretty-format'); import jestSnapshot = require('../index'); -import {printSnapshotAndReceived} from '../printSnapshot'; +import { + printPropertiesAndReceived, + printSnapshotAndReceived, +} from '../printSnapshot'; import {serialize} from '../utils'; const convertAnsi = (val: string): string => @@ -599,6 +602,29 @@ describe('pass true', () => { }); }); +describe('printPropertiesAndReceived', () => { + test('omit missing properties', () => { + const received = { + b: {}, + branchMap: {}, + f: {}, + fnMap: {}, + //hash: '0123456789abcdef', + path: '…', + s: {}, + statementMap: {}, + }; + const properties = { + hash: expect.any(String), + path: expect.any(String), + }; + + expect( + printPropertiesAndReceived(properties, received, false), + ).toMatchSnapshot(); + }); +}); + describe('printSnapshotAndReceived', () => { // Simulate default serialization. const testWithStringify = ( diff --git a/packages/jest-snapshot/src/printSnapshot.ts b/packages/jest-snapshot/src/printSnapshot.ts index cd83d3fd7fe6..465a6e094c48 100644 --- a/packages/jest-snapshot/src/printSnapshot.ts +++ b/packages/jest-snapshot/src/printSnapshot.ts @@ -6,6 +6,7 @@ */ import chalk = require('chalk'); +import {getObjectSubset} from 'expect/build/utils'; import { DIFF_DELETE, DIFF_EQUAL, @@ -145,7 +146,7 @@ export const printPropertiesAndReceived = ( if (isLineDiffable(properties) && isLineDiffable(received)) { return diffLinesUnified( serialize(properties).split('\n'), - serialize(received).split('\n'), + serialize(getObjectSubset(received, properties)).split('\n'), { aAnnotation, aColor: EXPECTED_COLOR, From d541c1e99891ac28efa578663a6878b8fa1e35ef Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 17 Nov 2019 14:33:20 -0500 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46632517999e..2d0231bff3f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ - `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880)) - `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898)) - `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049)) +- `[jest-snapshot]` Omit irrelevant `received` properties ([#9198](https://github.com/facebook/jest/pull/9198)) - `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890)) - `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058)) - `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) From eb228123d354d82985820c1cc57303ef025e6052 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 17 Nov 2019 15:06:41 -0500 Subject: [PATCH 3/5] Edit comment in test that hash is missing --- packages/jest-snapshot/src/__tests__/printSnapshot.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index 47d1c58b376c..b3bcab6d85df 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -609,7 +609,7 @@ describe('printPropertiesAndReceived', () => { branchMap: {}, f: {}, fnMap: {}, - //hash: '0123456789abcdef', + // hash is missing path: '…', s: {}, statementMap: {}, From 70a3cda021ea03f9aa3a344b189f83c608718049 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 17 Nov 2019 15:50:41 -0500 Subject: [PATCH 4/5] Edit CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d0231bff3f1..0edeb42e11c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,7 @@ - `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880)) - `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898)) - `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049)) -- `[jest-snapshot]` Omit irrelevant `received` properties ([#9198](https://github.com/facebook/jest/pull/9198)) +- `[jest-snapshot]` Omit irrelevant `received` properties when property matchers fail ([#9198](https://github.com/facebook/jest/pull/9198)) - `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890)) - `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058)) - `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) From 3eb4fd782ea625a13442e4ad898e73607ec329d6 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 17 Nov 2019 16:03:43 -0500 Subject: [PATCH 5/5] Add comment to explain import hack --- packages/jest-snapshot/src/printSnapshot.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/jest-snapshot/src/printSnapshot.ts b/packages/jest-snapshot/src/printSnapshot.ts index 465a6e094c48..8ba097570f5c 100644 --- a/packages/jest-snapshot/src/printSnapshot.ts +++ b/packages/jest-snapshot/src/printSnapshot.ts @@ -6,6 +6,9 @@ */ import chalk = require('chalk'); +// Temporary hack because getObjectSubset has known limitations, +// is not in the public interface of the expect package, +// and the long-term goal is to use a non-serialization diff. import {getObjectSubset} from 'expect/build/utils'; import { DIFF_DELETE,