Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(matcher-utils): correct diff for expect asymmetric matchers #12264

Merged
merged 1 commit into from Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Expand Up @@ -2175,8 +2175,15 @@ Received: <r>{"0": "a", "1": "b", "2": "c"}</>
exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContaining {"a": 2}) 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expected: <g>ObjectContaining {"a": 2}</>
Received: <r>{"a": 1, "b": 2}</>
<g>- Expected - 2</>
<r>+ Received + 3</>

<g>- ObjectContaining {</>
<g>- "a": 2,</>
<r>+ Object {</>
<r>+ "a": 1,</>
<r>+ "b": 2,</>
<d> }</>
`;

exports[`.toEqual() {pass: false} expect({"a": 1}).toEqual({"a": 2}) 1`] = `
Expand Down Expand Up @@ -3560,6 +3567,14 @@ Expected path: <g>"memo"</>
Expected value: not <g>[]</>
`;

exports[`.toHaveProperty() {pass: true} expect({"": 1}).toHaveProperty('', 1) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>, </><g>value</><d>)</>

Expected path: <g>""</>

Expected value: not <g>1</>
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [[{"c": [{"d": 1}]}]]}}).toHaveProperty('a.b[0][0].c[0].d', 1) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveProperty<d>(</><g>path</><d>, </><g>value</><d>)</>

Expand Down
Expand Up @@ -88,6 +88,25 @@ exports[`ensureNumbers() with options promise resolves isNot true expected 1`] =
Expected has value: <g>null</>
`;

exports[`printDiffOrStringify expected asymmetric matchers should be diffable 1`] = `
<g>- Expected - 2</>
<r>+ Received + 2</>

<g>- ObjectContaining {</>
<r>+ Object {</>
<d> "array": Array [</>
<d> Object {</>
<d> "3": "three",</>
<d> "four": "4",</>
<d> "one": 1,</>
<g>- "two": 2,</>
<r>+ "two": 1,</>
<d> },</>
<d> ],</>
<d> "foo": "bar",</>
<d> }</>
`;

exports[`stringify() toJSON errors when comparing two objects 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expand Down
34 changes: 34 additions & 0 deletions packages/jest-matcher-utils/src/__tests__/index.test.ts
Expand Up @@ -342,3 +342,37 @@ describe('matcherHint', () => {
expect(received).toMatch(substringPositive);
});
});

describe('printDiffOrStringify', () => {
test('expected asymmetric matchers should be diffable', () => {
jest.dontMock('jest-diff');
jest.resetModules();
const {printDiffOrStringify} = require('../');

const expected = expect.objectContaining({
array: [
{
3: 'three',
four: '4',
one: 1,
two: 2,
},
],
foo: 'bar',
});
const received = {
array: [
{
3: 'three',
four: '4',
one: 1,
two: 1,
},
],
foo: 'bar',
};
expect(
printDiffOrStringify(expected, received, 'Expected', 'Received', false),
).toMatchSnapshot();
});
});
7 changes: 0 additions & 7 deletions packages/jest-matcher-utils/src/index.ts
Expand Up @@ -293,13 +293,6 @@ const isLineDiffable = (expected: unknown, received: unknown): boolean => {
return false;
}

if (
expectedType === 'object' &&
typeof (expected as any).asymmetricMatch === 'function'
) {
return false;
}

if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems odd to me to remove the check for expectedType, but not receivedType? Although I guess passing an asymmetric matcher as received is weird, so maybe not

receivedType === 'object' &&
typeof (received as any).asymmetricMatch === 'function'
Expand Down