Skip to content

Commit 25894ad

Browse files
authored
fix(jest-snapshot): do not highlight matched asymmetricMatcher in diffs (#13491)
1 parent abd7d83 commit 25894ad

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[@jest/test-sequencer]` Make sure sharding does not produce empty groups ([#13476](https://github.com/facebook/jest/pull/13476))
88
- `[jest-circus]` Test marked as `todo` are shown as todo when inside a focussed describe ([#13504](https://github.com/facebook/jest/pull/13504))
99
- `[jest-mock]` Ensure mock resolved and rejected values are promises from correct realm ([#13503](https://github.com/facebook/jest/pull/13503))
10+
- `[jest-snapshot]` Don't highlight passing asymmetric property matchers in snapshot diff ([#13480](https://github.com/facebook/jest/issues/13480))
1011

1112
### Chore & Maintenance
1213

packages/jest-matcher-utils/src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,7 @@ export const printDiffOrStringify = (
363363

364364
if (isLineDiffable(expected, received)) {
365365
const {replacedExpected, replacedReceived} =
366-
replaceMatchedToAsymmetricMatcher(
367-
deepCyclicCopyReplaceable(expected),
368-
deepCyclicCopyReplaceable(received),
369-
[],
370-
[],
371-
);
366+
replaceMatchedToAsymmetricMatcher(expected, received, [], []);
372367
const difference = diffDefault(replacedExpected, replacedReceived, {
373368
aAnnotation: expectedLabel,
374369
bAnnotation: receivedLabel,
@@ -412,7 +407,21 @@ const shouldPrintDiff = (actual: unknown, expected: unknown) => {
412407
return true;
413408
};
414409

415-
function replaceMatchedToAsymmetricMatcher(
410+
export function replaceMatchedToAsymmetricMatcher(
411+
replacedExpected: unknown,
412+
replacedReceived: unknown,
413+
expectedCycles: Array<unknown>,
414+
receivedCycles: Array<unknown>,
415+
): {replacedExpected: unknown; replacedReceived: unknown} {
416+
return _replaceMatchedToAsymmetricMatcher(
417+
deepCyclicCopyReplaceable(replacedExpected),
418+
deepCyclicCopyReplaceable(replacedReceived),
419+
expectedCycles,
420+
receivedCycles,
421+
);
422+
}
423+
424+
function _replaceMatchedToAsymmetricMatcher(
416425
replacedExpected: unknown,
417426
replacedReceived: unknown,
418427
expectedCycles: Array<unknown>,
@@ -446,7 +455,7 @@ function replaceMatchedToAsymmetricMatcher(
446455
expectedReplaceable.set(key, receivedValue);
447456
}
448457
} else if (Replaceable.isReplaceable(expectedValue, receivedValue)) {
449-
const replaced = replaceMatchedToAsymmetricMatcher(
458+
const replaced = _replaceMatchedToAsymmetricMatcher(
450459
expectedValue,
451460
receivedValue,
452461
expectedCycles,

packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,23 @@ Received: <t>"received"</>
259259
`;
260260

261261
exports[`printPropertiesAndReceived omit missing properties 1`] = `
262-
<g>- Expected properties - 2</>
263-
<r>+ Received value + 1</>
262+
<g>- Expected properties - 1</>
263+
<r>+ Received value + 0</>
264264

265265
<d> Object {</>
266266
<g>- "hash": Any<String>,</>
267-
<g>- "path": Any<String>,</>
268-
<r>+ "path": "…",</>
267+
<d> "path": Any<String>,</>
268+
<d> }</>
269+
`;
270+
271+
exports[`printPropertiesAndReceived only highlight non passing properties 1`] = `
272+
<g>- Expected properties - 1</>
273+
<r>+ Received value + 1</>
274+
275+
<d> Object {</>
276+
<d> "a": Any<Number>,</>
277+
<g>- "b": Any<Number>,</>
278+
<r>+ "b": "some string",</>
269279
<d> }</>
270280
`;
271281

packages/jest-snapshot/src/__tests__/printSnapshot.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,21 @@ describe('printPropertiesAndReceived', () => {
809809
printPropertiesAndReceived(properties, received, false),
810810
).toMatchSnapshot();
811811
});
812+
813+
test('only highlight non passing properties', () => {
814+
const received = {
815+
a: 1,
816+
b: 'some string',
817+
c: 'another string',
818+
};
819+
const properties = {
820+
a: expect.any(Number),
821+
b: expect.any(Number),
822+
};
823+
expect(
824+
printPropertiesAndReceived(properties, received, false),
825+
).toMatchSnapshot();
826+
});
812827
});
813828

814829
describe('printSnapshotAndReceived', () => {

packages/jest-snapshot/src/printSnapshot.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
RECEIVED_COLOR,
2828
getLabelPrinter,
2929
matcherHint,
30+
replaceMatchedToAsymmetricMatcher,
3031
} from 'jest-matcher-utils';
3132
import {format as prettyFormat} from 'pretty-format';
3233
import {
@@ -205,9 +206,13 @@ export const printPropertiesAndReceived = (
205206
const bAnnotation = 'Received value';
206207

207208
if (isLineDiffable(properties) && isLineDiffable(received)) {
209+
const {replacedExpected, replacedReceived} =
210+
replaceMatchedToAsymmetricMatcher(properties, received, [], []);
208211
return diffLinesUnified(
209-
serialize(properties).split('\n'),
210-
serialize(getObjectSubset(received, properties)).split('\n'),
212+
serialize(replacedExpected).split('\n'),
213+
serialize(getObjectSubset(replacedReceived, replacedExpected)).split(
214+
'\n',
215+
),
211216
{
212217
aAnnotation,
213218
aColor: EXPECTED_COLOR,

0 commit comments

Comments
 (0)