Skip to content

Commit

Permalink
fix(matcher-utils): correct diff for expect asymmetric matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnormo committed Jan 29, 2022
1 parent d1bc333 commit 61a4a74
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
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 (
receivedType === 'object' &&
typeof (received as any).asymmetricMatch === 'function'
Expand Down

0 comments on commit 61a4a74

Please sign in to comment.