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 28, 2022
1 parent 056e5ef commit 7652055
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
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 7652055

Please sign in to comment.