Skip to content

Commit

Permalink
Fixes jestjs#13480
Browse files Browse the repository at this point in the history
  • Loading branch information
hthorhalls committed Oct 24, 2022
1 parent abd7d83 commit d0d1cad
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
25 changes: 17 additions & 8 deletions packages/jest-matcher-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -412,7 +407,21 @@ const shouldPrintDiff = (actual: unknown, expected: unknown) => {
return true;
};

function replaceMatchedToAsymmetricMatcher(
export const replaceMatchedToAsymmetricMatcher = (
replacedExpected: unknown,
replacedReceived: unknown,
expectedCycles: Array<unknown>,
receivedCycles: Array<unknown>,
): any => {
return _replaceMatchedToAsymmetricMatcher(
deepCyclicCopyReplaceable(replacedExpected),
deepCyclicCopyReplaceable(replacedReceived),
expectedCycles,
receivedCycles,
);
};

function _replaceMatchedToAsymmetricMatcher(
replacedExpected: unknown,
replacedReceived: unknown,
expectedCycles: Array<unknown>,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,23 @@ Received: <t>"received"</>
`;

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

<d> Object {</>
<g>- "hash": Any<String>,</>
<g>- "path": Any<String>,</>
<r>+ "path": "…",</>
<d> "path": Any<String>,</>
<d> }</>
`;

exports[`printPropertiesAndReceived only highlight non passing properties 1`] = `
<g>- Expected properties - 1</>
<r>+ Received value + 1</>

<d> Object {</>
<d> "a": Any<Number>,</>
<g>- "b": Any<Number>,</>
<r>+ "b": "some string",</>
<d> }</>
`;

Expand Down
15 changes: 15 additions & 0 deletions packages/jest-snapshot/src/__tests__/printSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-snapshot/src/printSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
RECEIVED_COLOR,
getLabelPrinter,
matcherHint,
replaceMatchedToAsymmetricMatcher,
} from 'jest-matcher-utils';
import {format as prettyFormat} from 'pretty-format';
import {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d0d1cad

Please sign in to comment.