Skip to content

Commit

Permalink
Support varargs in toHaveBeenCalledOnceWith (closes #517) (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
keeganwitt committed Feb 2, 2023
1 parent 6fbb99e commit 485cf45
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/matchers/toHaveBeenCalledOnceWith.js
@@ -1,6 +1,6 @@
import { isJestMockOrSpy } from '../utils';

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

if (!isJestMockOrSpy(received)) {
Expand All @@ -16,7 +16,7 @@ export function toHaveBeenCalledOnceWith(received, expected) {
}

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

return {
pass,
Expand All @@ -26,7 +26,7 @@ export function toHaveBeenCalledOnceWith(received, expected) {
matcherHint('.not.toHaveBeenCalledOnceWith') +
'\n\n' +
`Expected mock function to have been called any amount of times but one with ${printExpected(
expected,
received.mock.calls[0],
)}, but it was called exactly once with ${printExpected(expected)}.`
);
}
Expand Down
Expand Up @@ -3,7 +3,7 @@
exports[`.not.toHaveBeenCalledOnceWith fails if mock was invoked exactly once with the expected value 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toHaveBeenCalledOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called any amount of times but one with <green>"hello"</color>, but it was called exactly once with <green>"hello"</color>."
Expected mock function to have been called any amount of times but one with <green>["hello"]</color>, but it was called exactly once with <green>["hello"]</color>."
`;

exports[`.toHaveBeenCalledOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = `
Expand Down Expand Up @@ -32,6 +32,6 @@ Received has value: <red>[Function mock1]</color>"
exports[`.toHaveBeenCalledOnceWith fails when given value is not the expected one 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once with <red>"hello"</color>, but it was called with:
Expected mock function to have been called exactly once with <red>["hello"]</color>, but it was called with:
<red>"not hello"</color>"
`;
20 changes: 20 additions & 0 deletions test/matchers/toHaveBeenCalledOnceWith.test.js
Expand Up @@ -13,6 +13,16 @@ describe('.toHaveBeenCalledOnceWith', () => {
expect(mock).toHaveBeenCalledOnceWith('hello');
});

test('passes if mock was invoked exactly once with the expected values in an array', () => {
mock(['hello', 'there']);
expect(mock).toHaveBeenCalledOnceWith(['hello', 'there']);
});

test('passes if mock was invoked exactly once with the expected values', () => {
mock('hello', 'there');
expect(mock).toHaveBeenCalledOnceWith('hello', 'there');
});

test('fails if mock was never invoked indicating that it was invoked 0 times', () => {
expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot();
});
Expand Down Expand Up @@ -59,4 +69,14 @@ describe('.not.toHaveBeenCalledOnceWith', () => {
mock('not hello');
expect(mock).not.toHaveBeenCalledOnceWith('hello');
});

test('passes if mock was invoked exactly once without both expected values in an array', () => {
mock(['hello', 'there']);
expect(mock).not.toHaveBeenCalledOnceWith(['hello', 'not there']);
});

test('passes if mock was invoked exactly once without both expected values', () => {
mock('hello', 'there');
expect(mock).not.toHaveBeenCalledOnceWith('hello', 'not there');
});
});
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -600,7 +600,7 @@ declare namespace jest {
/**
* Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time with the expected value.
*/
toHaveBeenCalledOnceWith(arg: unknown): R;
toHaveBeenCalledOnceWith(...args: unknown[]): R;

/**
* Use `.toBeNumber` when checking if a value is a `Number`.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/matchers/Mock.mdx
Expand Up @@ -50,7 +50,7 @@ Use `.toHaveBeenCalledOnce` to check if a `Mock` was called exactly one time.

### .toHaveBeenCalledOnceWith()

Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time with the expected value.
Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time with the expected values.

<TestFile name="toHaveBeenCalledOnceWith">
{`test('passes only if mock was called exactly once with the expected value', () => {
Expand Down

0 comments on commit 485cf45

Please sign in to comment.