diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 400e8b6e424d..5a3e468bb84b 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -3479,6 +3479,30 @@ Received: \\"bar\\"" `; +exports[`.toStrictEqual() matches the expected snapshot when it fails 1`] = ` +"expect(received).toStrictEqual(expected) + +Difference: + +- Expected ++ Received + + Object { +- \\"test\\": TestClassA { +- \\"a\\": 1, +- \\"b\\": 2, +- }, ++ \\"test\\": 2, + }" +`; + +exports[`.toStrictEqual() matches the expected snapshot when it fails 2`] = ` +"expect(received).not.toStrictEqual(expected) + +Expected: {\\"test\\": {\\"a\\": 1, \\"b\\": 2}} +Received: {\\"test\\": {\\"a\\": 1, \\"b\\": 2}}" +`; + exports[`toMatchObject() {pass: false} expect([0]).toMatchObject([-0]) 1`] = ` "expect(received).toMatchObject(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index e695eec418ed..344e451b55e4 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -244,6 +244,20 @@ describe('.toStrictEqual()', () => { }).toStrictEqual({test: new TestClassA(1, 2)}); }); + it('matches the expected snapshot when it fails', () => { + expect(() => + jestExpect({ + test: 2, + }).toStrictEqual({test: new TestClassA(1, 2)}), + ).toThrowErrorMatchingSnapshot(); + + expect(() => + jestExpect({ + test: new TestClassA(1, 2), + }).not.toStrictEqual({test: new TestClassA(1, 2)}), + ).toThrowErrorMatchingSnapshot(); + }); + it('does not pass for different types', () => { expect({ test: new TestClassA(1, 2), diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 215114de52d5..043032bc0803 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -628,27 +628,20 @@ const matchers: MatchersObject = { true, ); + const hint = matcherHint('.toStrictEqual', undefined, undefined, { + isNot: this.isNot, + }); const message = pass ? () => - matcherHint('.not.toStrictEqual') + + hint + '\n\n' + - `Expected value to not equal:\n` + - ` ${printExpected(expected)}\n` + - `Received:\n` + - ` ${printReceived(received)}` + `Expected: ${printExpected(expected)}\n` + + `Received: ${printReceived(received)}` : () => { const diffString = diff(expected, received, { expand: this.expand, }); - return ( - matcherHint('.toStrictEqual') + - '\n\n' + - `Expected value to equal:\n` + - ` ${printExpected(expected)}\n` + - `Received:\n` + - ` ${printReceived(received)}` + - (diffString ? `\n\nDifference:\n\n${diffString}` : '') - ); + return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : ''); }; // Passing the the actual and expected objects so that a custom reporter