Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect: Display equal values for ReturnedWith similar to CalledWith #8791

Merged
merged 2 commits into from Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
Expand Up @@ -175,7 +175,7 @@ exports[`lastReturnedWith lastReturnedWith works with three calls 1`] = `
Expected: not <green>\\"foo3\\"</>
Received
2: <red>\\"foo2\\"</>
-> 3: <red>\\"foo3\\"</>
-> 3: <dim>\\"foo3\\"</>

Number of returns: <red>3</>"
`;
Expand Down Expand Up @@ -493,7 +493,7 @@ n: 3
Expected: not <green>1</>
Received
2: function call has not returned yet
-> 3: <red>1</>
-> 3: <dim>1</>
4: <red>0</>

Number of returns: <red>2</>
Expand All @@ -507,7 +507,7 @@ n: 4
Expected: not <green>0</>
Received
3: <red>1</>
-> 4: <red>0</>
-> 4: <dim>0</>

Number of returns: <red>2</>
Number of calls: <red>4</>"
Expand Down Expand Up @@ -545,7 +545,7 @@ exports[`nthReturnedWith nthReturnedWith should reject nth value greater than nu
n: 4
Expected: <green>\\"foo\\"</>
Received
3: <red>\\"foo\\"</>
3: <dim>\\"foo\\"</>

Number of returns: <red>3</>"
`;
Expand All @@ -568,7 +568,7 @@ exports[`nthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with first
n: 1
Expected: not <green>\\"foo1\\"</>
Received
-> 1: <red>\\"foo1\\"</>
-> 1: <dim>\\"foo1\\"</>
2: <red>\\"foo2\\"</>

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

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

Number of returns: <red>3</>"
`;
Expand Down Expand Up @@ -1831,7 +1831,7 @@ n: 3
Expected: not <green>1</>
Received
2: function call has not returned yet
-> 3: <red>1</>
-> 3: <dim>1</>
4: <red>0</>

Number of returns: <red>2</>
Expand All @@ -1845,7 +1845,7 @@ n: 4
Expected: not <green>0</>
Received
3: <red>1</>
-> 4: <red>0</>
-> 4: <dim>0</>

Number of returns: <red>2</>
Number of calls: <red>4</>"
Expand Down Expand Up @@ -1883,7 +1883,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should reject nth value greater t
n: 4
Expected: <green>\\"foo\\"</>
Received
3: <red>\\"foo\\"</>
3: <dim>\\"foo\\"</>

Number of returns: <red>3</>"
`;
Expand All @@ -1906,7 +1906,7 @@ exports[`toHaveNthReturnedWith nthReturnedWith should replace 1st, 2nd, 3rd with
n: 1
Expected: not <green>\\"foo1\\"</>
Received
-> 1: <red>\\"foo1\\"</>
-> 1: <dim>\\"foo1\\"</>
2: <red>\\"foo2\\"</>

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

Number of returns: <red>3</>"
Expand Down
20 changes: 14 additions & 6 deletions packages/expect/src/spyMatchers.ts
Expand Up @@ -61,11 +61,10 @@ const isEqualValue = (expected: unknown, received: unknown): boolean =>
const isEqualCall = (
expected: Array<unknown>,
received: Array<unknown>,
): 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<any>): number =>
results.reduce(
Expand Down Expand Up @@ -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];
Expand All @@ -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<IndexedResult>,
isOnlyCall: boolean,
iExpectedCall?: number,
Expand All @@ -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);
Expand All @@ -347,7 +349,7 @@ const printReceivedResults = (
(printed: string, [i, result]: IndexedResult) =>
printed +
printAligned(String(i + 1), i === iExpectedCall) +
printResult(result) +
printResult(result, expected) +
'\n',
'',
)
Expand Down Expand Up @@ -656,6 +658,7 @@ const createToReturnWithMatcher = (matcherName: string) =>
? ''
: printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
)) +
Expand All @@ -677,6 +680,7 @@ const createToReturnWithMatcher = (matcherName: string) =>
`Expected: ${printExpected(expected)}\n` +
printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
) +
Expand Down Expand Up @@ -809,6 +813,7 @@ const createLastReturnedMatcher = (matcherName: string) =>
? ''
: printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
iLast,
Expand Down Expand Up @@ -841,6 +846,7 @@ const createLastReturnedMatcher = (matcherName: string) =>
`Expected: ${printExpected(expected)}\n` +
printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
iLast,
Expand Down Expand Up @@ -1037,6 +1043,7 @@ const createNthReturnedWithMatcher = (matcherName: string) =>
? ''
: printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
iNth,
Expand Down Expand Up @@ -1097,6 +1104,7 @@ const createNthReturnedWithMatcher = (matcherName: string) =>
`Expected: ${printExpected(expected)}\n` +
printReceivedResults(
'Received: ',
expected,
indexedResults,
results.length === 1,
iNth,
Expand Down