Skip to content

Commit 9a15d46

Browse files
authored
expect: Display equal values for ReturnedWith similar to CalledWith (#8791)
* expect: Display equal values for ReturnedWith similar to CalledWith * Update CHANGELOG.md
1 parent 384fe4a commit 9a15d46

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[expect]` Throw matcher error when received cannot be jasmine spy ([#8747](https://github.com/facebook/jest/pull/8747))
1212
- `[expect]` Improve report when negative CalledWith assertion fails ([#8755](https://github.com/facebook/jest/pull/8755))
1313
- `[expect]` Improve report when positive CalledWith assertion fails ([#8771](https://github.com/facebook/jest/pull/8771))
14+
- `[expect]` Display equal values for ReturnedWith similar to CalledWith ([#8791](https://github.com/facebook/jest/pull/8791))
1415
- `[jest-snapshot]` Highlight substring differences when matcher fails, part 3 ([#8569](https://github.com/facebook/jest/pull/8569))
1516
- `[jest-core]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665))
1617
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454))

packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ exports[`lastReturnedWith lastReturnedWith works with three calls 1`] = `
175175
Expected: not <green>\\"foo3\\"</>
176176
Received
177177
2: <red>\\"foo2\\"</>
178-
-> 3: <red>\\"foo3\\"</>
178+
-> 3: <dim>\\"foo3\\"</>
179179

180180
Number of returns: <red>3</>"
181181
`;
@@ -493,7 +493,7 @@ n: 3
493493
Expected: not <green>1</>
494494
Received
495495
2: function call has not returned yet
496-
-> 3: <red>1</>
496+
-> 3: <dim>1</>
497497
4: <red>0</>
498498

499499
Number of returns: <red>2</>
@@ -507,7 +507,7 @@ n: 4
507507
Expected: not <green>0</>
508508
Received
509509
3: <red>1</>
510-
-> 4: <red>0</>
510+
-> 4: <dim>0</>
511511

512512
Number of returns: <red>2</>
513513
Number of calls: <red>4</>"
@@ -545,7 +545,7 @@ exports[`nthReturnedWith nthReturnedWith should reject nth value greater than nu
545545
n: 4
546546
Expected: <green>\\"foo\\"</>
547547
Received
548-
3: <red>\\"foo\\"</>
548+
3: <dim>\\"foo\\"</>
549549

550550
Number of returns: <red>3</>"
551551
`;
@@ -568,7 +568,7 @@ exports[`nthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with first
568568
n: 1
569569
Expected: not <green>\\"foo1\\"</>
570570
Received
571-
-> 1: <red>\\"foo1\\"</>
571+
-> 1: <dim>\\"foo1\\"</>
572572
2: <red>\\"foo2\\"</>
573573

574574
Number of returns: <red>3</>"
@@ -580,7 +580,7 @@ exports[`nthReturnedWith nthReturnedWith works with three calls 1`] = `
580580
n: 1
581581
Expected: not <green>\\"foo1\\"</>
582582
Received
583-
-> 1: <red>\\"foo1\\"</>
583+
-> 1: <dim>\\"foo1\\"</>
584584
2: <red>\\"foo2\\"</>
585585

586586
Number of returns: <red>3</>"
@@ -1669,7 +1669,7 @@ exports[`toHaveLastReturnedWith lastReturnedWith works with three calls 1`] = `
16691669
Expected: not <green>\\"foo3\\"</>
16701670
Received
16711671
2: <red>\\"foo2\\"</>
1672-
-> 3: <red>\\"foo3\\"</>
1672+
-> 3: <dim>\\"foo3\\"</>
16731673

16741674
Number of returns: <red>3</>"
16751675
`;
@@ -1831,7 +1831,7 @@ n: 3
18311831
Expected: not <green>1</>
18321832
Received
18331833
2: function call has not returned yet
1834-
-> 3: <red>1</>
1834+
-> 3: <dim>1</>
18351835
4: <red>0</>
18361836

18371837
Number of returns: <red>2</>
@@ -1845,7 +1845,7 @@ n: 4
18451845
Expected: not <green>0</>
18461846
Received
18471847
3: <red>1</>
1848-
-> 4: <red>0</>
1848+
-> 4: <dim>0</>
18491849

18501850
Number of returns: <red>2</>
18511851
Number of calls: <red>4</>"
@@ -1883,7 +1883,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should reject nth value greater t
18831883
n: 4
18841884
Expected: <green>\\"foo\\"</>
18851885
Received
1886-
3: <red>\\"foo\\"</>
1886+
3: <dim>\\"foo\\"</>
18871887

18881888
Number of returns: <red>3</>"
18891889
`;
@@ -1906,7 +1906,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with
19061906
n: 1
19071907
Expected: not <green>\\"foo1\\"</>
19081908
Received
1909-
-> 1: <red>\\"foo1\\"</>
1909+
-> 1: <dim>\\"foo1\\"</>
19101910
2: <red>\\"foo2\\"</>
19111911

19121912
Number of returns: <red>3</>"
@@ -1918,7 +1918,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith works with three calls 1`] = `
19181918
n: 1
19191919
Expected: not <green>\\"foo1\\"</>
19201920
Received
1921-
-> 1: <red>\\"foo1\\"</>
1921+
-> 1: <dim>\\"foo1\\"</>
19221922
2: <red>\\"foo2\\"</>
19231923

19241924
Number of returns: <red>3</>"

packages/expect/src/spyMatchers.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ const isEqualValue = (expected: unknown, received: unknown): boolean =>
6161
const isEqualCall = (
6262
expected: Array<unknown>,
6363
received: Array<unknown>,
64-
): boolean => equals(expected, received, [iterableEquality]);
64+
): boolean => isEqualValue(expected, received);
6565

6666
const isEqualReturn = (expected: unknown, result: any): boolean =>
67-
result.type === 'return' &&
68-
equals(expected, result.value, [iterableEquality]);
67+
result.type === 'return' && isEqualValue(expected, result.value);
6968

7069
const countReturns = (results: Array<any>): number =>
7170
results.reduce(
@@ -313,11 +312,13 @@ const isLineDiffableArg = (expected: unknown, received: unknown): boolean => {
313312
return true;
314313
};
315314

316-
const printResult = (result: any) =>
315+
const printResult = (result: any, expected: unknown) =>
317316
result.type === 'throw'
318317
? 'function call threw an error'
319318
: result.type === 'incomplete'
320319
? 'function call has not returned yet'
320+
: isEqualValue(expected, result.value)
321+
? printCommon(result.value)
321322
: printReceived(result.value);
322323

323324
type IndexedResult = [number, any];
@@ -326,6 +327,7 @@ type IndexedResult = [number, any];
326327
// so additional empty line can separate from `Number of returns` which follows.
327328
const printReceivedResults = (
328329
label: string,
330+
expected: unknown,
329331
indexedResults: Array<IndexedResult>,
330332
isOnlyCall: boolean,
331333
iExpectedCall?: number,
@@ -335,7 +337,7 @@ const printReceivedResults = (
335337
}
336338

337339
if (isOnlyCall && (iExpectedCall === 0 || iExpectedCall === undefined)) {
338-
return label + printResult(indexedResults[0][1]) + '\n';
340+
return label + printResult(indexedResults[0][1], expected) + '\n';
339341
}
340342

341343
const printAligned = getRightAlignedPrinter(label);
@@ -347,7 +349,7 @@ const printReceivedResults = (
347349
(printed: string, [i, result]: IndexedResult) =>
348350
printed +
349351
printAligned(String(i + 1), i === iExpectedCall) +
350-
printResult(result) +
352+
printResult(result, expected) +
351353
'\n',
352354
'',
353355
)
@@ -656,6 +658,7 @@ const createToReturnWithMatcher = (matcherName: string) =>
656658
? ''
657659
: printReceivedResults(
658660
'Received: ',
661+
expected,
659662
indexedResults,
660663
results.length === 1,
661664
)) +
@@ -677,6 +680,7 @@ const createToReturnWithMatcher = (matcherName: string) =>
677680
`Expected: ${printExpected(expected)}\n` +
678681
printReceivedResults(
679682
'Received: ',
683+
expected,
680684
indexedResults,
681685
results.length === 1,
682686
) +
@@ -809,6 +813,7 @@ const createLastReturnedMatcher = (matcherName: string) =>
809813
? ''
810814
: printReceivedResults(
811815
'Received: ',
816+
expected,
812817
indexedResults,
813818
results.length === 1,
814819
iLast,
@@ -841,6 +846,7 @@ const createLastReturnedMatcher = (matcherName: string) =>
841846
`Expected: ${printExpected(expected)}\n` +
842847
printReceivedResults(
843848
'Received: ',
849+
expected,
844850
indexedResults,
845851
results.length === 1,
846852
iLast,
@@ -1037,6 +1043,7 @@ const createNthReturnedWithMatcher = (matcherName: string) =>
10371043
? ''
10381044
: printReceivedResults(
10391045
'Received: ',
1046+
expected,
10401047
indexedResults,
10411048
results.length === 1,
10421049
iNth,
@@ -1097,6 +1104,7 @@ const createNthReturnedWithMatcher = (matcherName: string) =>
10971104
`Expected: ${printExpected(expected)}\n` +
10981105
printReceivedResults(
10991106
'Received: ',
1107+
expected,
11001108
indexedResults,
11011109
results.length === 1,
11021110
iNth,

0 commit comments

Comments
 (0)