Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
include custom mock name in lastReturnedWith matcher
  • Loading branch information
leggsimon committed May 16, 2018
commit 246d8e07d1ecdeedeb9e95c1f139bb9702daf7e3
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Expected mock function to have been last called with:
exports[`lastReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).lastReturnedWith(</><green>expected</><dim>)</>

Expected mock function to have last returned:
Expected mock function \\"named-mock\\" to have last returned:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;
Expand Down Expand Up @@ -1354,7 +1354,7 @@ Expected mock function first call to have been called with:
exports[`toHaveLastReturnedWith includes the custom mock name in the error message 1`] = `
"<dim>expect(</><red>named-mock</><dim>).toHaveLastReturnedWith(</><green>expected</><dim>)</>

Expected mock function to have last returned:
Expected mock function \\"named-mock\\" to have last returned:
<green>\\"foo\\"</>
But it did <red>not return</>"
`;
Expand Down
9 changes: 7 additions & 2 deletions packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ const createLastReturnedMatcher = matcherName => (
) => {
ensureMock(received, matcherName);

const receivedName = received.getMockName();
const identifier = receivedName === 'jest.fn()'
? 'mock function'
: `mock function "${receivedName}"`;

const returnValues = received.mock.returnValues;
const lastReturnValue = returnValues[returnValues.length - 1];
const pass = equals(lastReturnValue, expected, [iterableEquality]);
Expand All @@ -268,14 +273,14 @@ const createLastReturnedMatcher = matcherName => (
? () =>
matcherHint('.not' + matcherName, received.getMockName()) +
'\n\n' +
'Expected mock function to not have last returned:\n' +
`Expected ${identifier} to not have last returned:\n` +
` ${printExpected(expected)}\n` +
`But it last returned exactly:\n` +
` ${printReceived(lastReturnValue)}`
: () =>
matcherHint(matcherName, received.getMockName()) +
'\n\n' +
'Expected mock function to have last returned:\n' +
`Expected ${identifier} to have last returned:\n` +
` ${printExpected(expected)}\n` +
(returnValues.length > 0
? `But it last returned:\n ${printReceived(lastReturnValue)}`
Expand Down