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

Improve toHaveBeenCalledExactlyOnceWith messages #675

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/twelve-beers-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jest-extended': patch
---

Improve toHaveBeenCalledExactlyOnceWith messages
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,5 +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>

<green>- Expected arguments - 1</color>
<red>+ Received arguments + 1</color>

<dim> Array [</intensity>
<dim> "hello",</intensity>
<green>- "where",</color>
<red>+ "there",</color>
<dim> ]</intensity>"
`;
5 changes: 5 additions & 0 deletions test/matchers/toHaveBeenCalledExactlyOnceWith.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ describe('.toHaveBeenCalledExactlyOnceWith', () => {
mock('not hello');
expect(() => expect(mock).toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot();
});

test('fails when one given value is not the expected one', () => {
mock('hello', 'there');
expect(() => expect(mock).toHaveBeenCalledExactlyOnceWith('hello', 'where')).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toHaveBeenCalledExactlyOnceWith', () => {
Expand Down