Skip to content

Commit

Permalink
Improve toHaveBeenCalledExactlyOnceWith messages
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall committed Jan 26, 2024
1 parent 4537f47 commit a817c2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/matchers/toHaveBeenCalledExactlyOnceWith.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isJestMockOrSpy } from '../utils';

export function toHaveBeenCalledExactlyOnceWith(received, ...expected) {
const { printReceived, printExpected, printWithType, matcherHint } = this.utils;
const { printReceived, printExpected, printDiffOrStringify, printWithType, matcherHint } = this.utils;

if (!isJestMockOrSpy(received)) {
return {
Expand All @@ -17,6 +17,7 @@ export function toHaveBeenCalledExactlyOnceWith(received, ...expected) {

const actual = received.mock.calls[0];
const invokedOnce = received.mock.calls.length === 1;
const oneArgument = actual?.length === 1 && expected.length === 1;
const pass = invokedOnce && this.equals(expected, actual);

return {
Expand All @@ -27,12 +28,13 @@ export function toHaveBeenCalledExactlyOnceWith(received, ...expected) {
'\n\n' +
'Expected mock to be invoked some number of times other than once or once with ' +
`arguments other than ${printExpected(expected)}, but was invoked ` +
`${printReceived(received.mock.calls.length)} times with ${printReceived(...actual)}`
`${printReceived(received.mock.calls.length)} times with ${printReceived(actual)}`
: matcherHint('.toHaveBeenCalledExactlyOnceWith') +
'\n\n' +
(invokedOnce
? 'Expected mock function to have been called exactly once with ' +
`${printExpected(expected)}, but it was called with ${printReceived(...actual)}`
? oneArgument
? printDiffOrStringify(expected[0], actual[0], 'Expected', 'Received', this.expand)
: printDiffOrStringify(expected, actual, 'Expected arguments', 'Received arguments', this.expand)
: 'Expected mock function to have been called exactly once, but it was called ' +
`${printReceived(received.mock.calls.length)} times`);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`.not.toHaveBeenCalledExactlyOnceWith fails if mock was invoked exactly once with the expected value 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toHaveBeenCalledExactlyOnceWith()</intensity>
Expected mock to be invoked some number of times other than once or once with arguments other than <green>["hello"]</color>, but was invoked <red>1</color> times with <red>"hello"</color>"
Expected mock to be invoked some number of times other than once or once with arguments other than <green>["hello"]</color>, but was invoked <red>1</color> times with <red>["hello"]</color>"
`;

exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = `
Expand All @@ -30,11 +30,19 @@ Received has value: <red>[Function mock1]</color>"
exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not the expected one 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once with <green>["hello"]</color>, but it was called with <red>"not hello"</color>"
Expected: <green>"hello"</color>
Received: <red>"<inverse>not </inverse>hello"</color>"
`;

exports[`.toHaveBeenCalledExactlyOnceWith fails when one given value is not the expected one 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once with <green>["hello", "where"]</color>, but it was called with <red>"hello"</color>"
<green>- Expected arguments - 1</color>
<red>+ Received arguments + 1</color>
<dim> Array [</intensity>
<dim> "hello",</intensity>
<green>- "where",</color>
<red>+ "there",</color>
<dim> ]</intensity>"
`;

0 comments on commit a817c2f

Please sign in to comment.