From dacef889ee0a2ea8080daa966a16c58d9350a2f1 Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Mon, 28 Nov 2022 12:13:51 -0500 Subject: [PATCH] Support varargs in toHaveBeenCalledOnceWith (closes #517) --- src/matchers/toHaveBeenCalledOnceWith.js | 6 +++--- .../toHaveBeenCalledOnceWith.test.js.snap | 4 ++-- .../matchers/toHaveBeenCalledOnceWith.test.js | 20 +++++++++++++++++++ types/index.d.ts | 2 +- website/docs/matchers/Mock.mdx | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/matchers/toHaveBeenCalledOnceWith.js b/src/matchers/toHaveBeenCalledOnceWith.js index 02627d7e..4e73068c 100644 --- a/src/matchers/toHaveBeenCalledOnceWith.js +++ b/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)) { @@ -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, @@ -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)}.` ); } diff --git a/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap index ab0a9bab..fdc2906d 100644 --- a/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap +++ b/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap @@ -3,7 +3,7 @@ exports[`.not.toHaveBeenCalledOnceWith fails if mock was invoked exactly once with the expected value 1`] = ` "expect(received).not.toHaveBeenCalledOnceWith(expected) -Expected mock function to have been called any amount of times but one with "hello", but it was called exactly once with "hello"." +Expected mock function to have been called any amount of times but one with ["hello"], but it was called exactly once with ["hello"]." `; exports[`.toHaveBeenCalledOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` @@ -32,6 +32,6 @@ Received has value: [Function mock1]" exports[`.toHaveBeenCalledOnceWith fails when given value is not the expected one 1`] = ` "expect(received).toHaveBeenCalledOnceWith(expected) -Expected mock function to have been called exactly once with "hello", but it was called with: +Expected mock function to have been called exactly once with ["hello"], but it was called with: "not hello"" `; diff --git a/test/matchers/toHaveBeenCalledOnceWith.test.js b/test/matchers/toHaveBeenCalledOnceWith.test.js index 23c3ac4f..3e1e8741 100644 --- a/test/matchers/toHaveBeenCalledOnceWith.test.js +++ b/test/matchers/toHaveBeenCalledOnceWith.test.js @@ -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(); }); @@ -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'); + }); }); diff --git a/types/index.d.ts b/types/index.d.ts index 8fbafed5..a12b9681 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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`. diff --git a/website/docs/matchers/Mock.mdx b/website/docs/matchers/Mock.mdx index 114b92ed..21803c67 100644 --- a/website/docs/matchers/Mock.mdx +++ b/website/docs/matchers/Mock.mdx @@ -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. {`test('passes only if mock was called exactly once with the expected value', () => {