From d0d1cad528cd20cde54a039f8f3f56a6710448f5 Mon Sep 17 00:00:00 2001 From: Halldor Thorhallsson Date: Fri, 21 Oct 2022 15:22:45 -0400 Subject: [PATCH] Fixes #13480 --- packages/jest-matcher-utils/src/index.ts | 25 +++++++++++++------ .../__snapshots__/printSnapshot.test.ts.snap | 18 ++++++++++--- .../src/__tests__/printSnapshot.test.ts | 15 +++++++++++ packages/jest-snapshot/src/printSnapshot.ts | 9 +++++-- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index 8985eab6ade8..4dc18aec096f 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -363,12 +363,7 @@ export const printDiffOrStringify = ( if (isLineDiffable(expected, received)) { const {replacedExpected, replacedReceived} = - replaceMatchedToAsymmetricMatcher( - deepCyclicCopyReplaceable(expected), - deepCyclicCopyReplaceable(received), - [], - [], - ); + replaceMatchedToAsymmetricMatcher(expected, received, [], []); const difference = diffDefault(replacedExpected, replacedReceived, { aAnnotation: expectedLabel, bAnnotation: receivedLabel, @@ -412,7 +407,21 @@ const shouldPrintDiff = (actual: unknown, expected: unknown) => { return true; }; -function replaceMatchedToAsymmetricMatcher( +export const replaceMatchedToAsymmetricMatcher = ( + replacedExpected: unknown, + replacedReceived: unknown, + expectedCycles: Array, + receivedCycles: Array, +): any => { + return _replaceMatchedToAsymmetricMatcher( + deepCyclicCopyReplaceable(replacedExpected), + deepCyclicCopyReplaceable(replacedReceived), + expectedCycles, + receivedCycles, + ); +}; + +function _replaceMatchedToAsymmetricMatcher( replacedExpected: unknown, replacedReceived: unknown, expectedCycles: Array, @@ -446,7 +455,7 @@ function replaceMatchedToAsymmetricMatcher( expectedReplaceable.set(key, receivedValue); } } else if (Replaceable.isReplaceable(expectedValue, receivedValue)) { - const replaced = replaceMatchedToAsymmetricMatcher( + const replaced = _replaceMatchedToAsymmetricMatcher( expectedValue, receivedValue, expectedCycles, 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 4fa135cd940a..b1d923f8275d 100644 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap @@ -259,13 +259,23 @@ Received: "received" `; exports[`printPropertiesAndReceived omit missing properties 1`] = ` -- Expected properties - 2 -+ Received value + 1 +- Expected properties - 1 ++ Received value + 0 Object { - "hash": Any, -- "path": Any, -+ "path": "…", + "path": Any, + } +`; + +exports[`printPropertiesAndReceived only highlight non passing properties 1`] = ` +- Expected properties - 1 ++ Received value + 1 + + Object { + "a": Any, +- "b": Any, ++ "b": "some string", } `; diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index f76eee2aa566..1a2ccfd638b2 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -809,6 +809,21 @@ describe('printPropertiesAndReceived', () => { printPropertiesAndReceived(properties, received, false), ).toMatchSnapshot(); }); + + test('only highlight non passing properties', () => { + const received = { + a: 1, + b: 'some string', + c: 'another string', + }; + const properties = { + a: expect.any(Number), + b: expect.any(Number), + }; + expect( + printPropertiesAndReceived(properties, received, false), + ).toMatchSnapshot(); + }); }); describe('printSnapshotAndReceived', () => { diff --git a/packages/jest-snapshot/src/printSnapshot.ts b/packages/jest-snapshot/src/printSnapshot.ts index b49a64492eca..5672bbaa9011 100644 --- a/packages/jest-snapshot/src/printSnapshot.ts +++ b/packages/jest-snapshot/src/printSnapshot.ts @@ -27,6 +27,7 @@ import { RECEIVED_COLOR, getLabelPrinter, matcherHint, + replaceMatchedToAsymmetricMatcher, } from 'jest-matcher-utils'; import {format as prettyFormat} from 'pretty-format'; import { @@ -205,9 +206,13 @@ export const printPropertiesAndReceived = ( const bAnnotation = 'Received value'; if (isLineDiffable(properties) && isLineDiffable(received)) { + const {replacedExpected, replacedReceived} = + replaceMatchedToAsymmetricMatcher(properties, received, [], []); return diffLinesUnified( - serialize(properties).split('\n'), - serialize(getObjectSubset(received, properties)).split('\n'), + serialize(replacedExpected).split('\n'), + serialize(getObjectSubset(replacedReceived, replacedExpected)).split( + '\n', + ), { aAnnotation, aColor: EXPECTED_COLOR,