From 9a15d46823bb612786aee9d3d09970a99836ad6d Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 10 Aug 2019 18:49:31 -0400 Subject: [PATCH] expect: Display equal values for ReturnedWith similar to CalledWith (#8791) * expect: Display equal values for ReturnedWith similar to CalledWith * Update CHANGELOG.md --- CHANGELOG.md | 1 + .../__snapshots__/spyMatchers.test.js.snap | 24 +++++++++---------- packages/expect/src/spyMatchers.ts | 20 +++++++++++----- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 849150ef222b..119fa802697d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - `[expect]` Throw matcher error when received cannot be jasmine spy ([#8747](https://github.com/facebook/jest/pull/8747)) - `[expect]` Improve report when negative CalledWith assertion fails ([#8755](https://github.com/facebook/jest/pull/8755)) - `[expect]` Improve report when positive CalledWith assertion fails ([#8771](https://github.com/facebook/jest/pull/8771)) +- `[expect]` Display equal values for ReturnedWith similar to CalledWith ([#8791](https://github.com/facebook/jest/pull/8791)) - `[jest-snapshot]` Highlight substring differences when matcher fails, part 3 ([#8569](https://github.com/facebook/jest/pull/8569)) - `[jest-core]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665)) - `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) diff --git a/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap index c22e8bdf327e..1fa46b12f5ea 100644 --- a/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap @@ -175,7 +175,7 @@ exports[`lastReturnedWith lastReturnedWith works with three calls 1`] = ` Expected: not \\"foo3\\" Received 2: \\"foo2\\" --> 3: \\"foo3\\" +-> 3: \\"foo3\\" Number of returns: 3" `; @@ -493,7 +493,7 @@ n: 3 Expected: not 1 Received 2: function call has not returned yet --> 3: 1 +-> 3: 1 4: 0 Number of returns: 2 @@ -507,7 +507,7 @@ n: 4 Expected: not 0 Received 3: 1 --> 4: 0 +-> 4: 0 Number of returns: 2 Number of calls: 4" @@ -545,7 +545,7 @@ exports[`nthReturnedWith nthReturnedWith should reject nth value greater than nu n: 4 Expected: \\"foo\\" Received - 3: \\"foo\\" + 3: \\"foo\\" Number of returns: 3" `; @@ -568,7 +568,7 @@ exports[`nthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with first n: 1 Expected: not \\"foo1\\" Received --> 1: \\"foo1\\" +-> 1: \\"foo1\\" 2: \\"foo2\\" Number of returns: 3" @@ -580,7 +580,7 @@ exports[`nthReturnedWith nthReturnedWith works with three calls 1`] = ` n: 1 Expected: not \\"foo1\\" Received --> 1: \\"foo1\\" +-> 1: \\"foo1\\" 2: \\"foo2\\" Number of returns: 3" @@ -1669,7 +1669,7 @@ exports[`toHaveLastReturnedWith lastReturnedWith works with three calls 1`] = ` Expected: not \\"foo3\\" Received 2: \\"foo2\\" --> 3: \\"foo3\\" +-> 3: \\"foo3\\" Number of returns: 3" `; @@ -1831,7 +1831,7 @@ n: 3 Expected: not 1 Received 2: function call has not returned yet --> 3: 1 +-> 3: 1 4: 0 Number of returns: 2 @@ -1845,7 +1845,7 @@ n: 4 Expected: not 0 Received 3: 1 --> 4: 0 +-> 4: 0 Number of returns: 2 Number of calls: 4" @@ -1883,7 +1883,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should reject nth value greater t n: 4 Expected: \\"foo\\" Received - 3: \\"foo\\" + 3: \\"foo\\" Number of returns: 3" `; @@ -1906,7 +1906,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with n: 1 Expected: not \\"foo1\\" Received --> 1: \\"foo1\\" +-> 1: \\"foo1\\" 2: \\"foo2\\" Number of returns: 3" @@ -1918,7 +1918,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith works with three calls 1`] = ` n: 1 Expected: not \\"foo1\\" Received --> 1: \\"foo1\\" +-> 1: \\"foo1\\" 2: \\"foo2\\" Number of returns: 3" diff --git a/packages/expect/src/spyMatchers.ts b/packages/expect/src/spyMatchers.ts index 890dfce1f18b..0a37636fc64a 100644 --- a/packages/expect/src/spyMatchers.ts +++ b/packages/expect/src/spyMatchers.ts @@ -61,11 +61,10 @@ const isEqualValue = (expected: unknown, received: unknown): boolean => const isEqualCall = ( expected: Array, received: Array, -): boolean => equals(expected, received, [iterableEquality]); +): boolean => isEqualValue(expected, received); const isEqualReturn = (expected: unknown, result: any): boolean => - result.type === 'return' && - equals(expected, result.value, [iterableEquality]); + result.type === 'return' && isEqualValue(expected, result.value); const countReturns = (results: Array): number => results.reduce( @@ -313,11 +312,13 @@ const isLineDiffableArg = (expected: unknown, received: unknown): boolean => { return true; }; -const printResult = (result: any) => +const printResult = (result: any, expected: unknown) => result.type === 'throw' ? 'function call threw an error' : result.type === 'incomplete' ? 'function call has not returned yet' + : isEqualValue(expected, result.value) + ? printCommon(result.value) : printReceived(result.value); type IndexedResult = [number, any]; @@ -326,6 +327,7 @@ type IndexedResult = [number, any]; // so additional empty line can separate from `Number of returns` which follows. const printReceivedResults = ( label: string, + expected: unknown, indexedResults: Array, isOnlyCall: boolean, iExpectedCall?: number, @@ -335,7 +337,7 @@ const printReceivedResults = ( } if (isOnlyCall && (iExpectedCall === 0 || iExpectedCall === undefined)) { - return label + printResult(indexedResults[0][1]) + '\n'; + return label + printResult(indexedResults[0][1], expected) + '\n'; } const printAligned = getRightAlignedPrinter(label); @@ -347,7 +349,7 @@ const printReceivedResults = ( (printed: string, [i, result]: IndexedResult) => printed + printAligned(String(i + 1), i === iExpectedCall) + - printResult(result) + + printResult(result, expected) + '\n', '', ) @@ -656,6 +658,7 @@ const createToReturnWithMatcher = (matcherName: string) => ? '' : printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, )) + @@ -677,6 +680,7 @@ const createToReturnWithMatcher = (matcherName: string) => `Expected: ${printExpected(expected)}\n` + printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, ) + @@ -809,6 +813,7 @@ const createLastReturnedMatcher = (matcherName: string) => ? '' : printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, iLast, @@ -841,6 +846,7 @@ const createLastReturnedMatcher = (matcherName: string) => `Expected: ${printExpected(expected)}\n` + printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, iLast, @@ -1037,6 +1043,7 @@ const createNthReturnedWithMatcher = (matcherName: string) => ? '' : printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, iNth, @@ -1097,6 +1104,7 @@ const createNthReturnedWithMatcher = (matcherName: string) => `Expected: ${printExpected(expected)}\n` + printReceivedResults( 'Received: ', + expected, indexedResults, results.length === 1, iNth,