From a817c2fab29f309384ab534327d30f4cdce4c3d9 Mon Sep 17 00:00:00 2001 From: Emily M Klassen Date: Thu, 25 Jan 2024 16:27:14 -0800 Subject: [PATCH] Improve toHaveBeenCalledExactlyOnceWith messages --- src/matchers/toHaveBeenCalledExactlyOnceWith.js | 10 ++++++---- .../toHaveBeenCalledExactlyOnceWith.test.js.snap | 14 +++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/matchers/toHaveBeenCalledExactlyOnceWith.js b/src/matchers/toHaveBeenCalledExactlyOnceWith.js index f72229d4..02ccbd99 100644 --- a/src/matchers/toHaveBeenCalledExactlyOnceWith.js +++ b/src/matchers/toHaveBeenCalledExactlyOnceWith.js @@ -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 { @@ -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 { @@ -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`); }, diff --git a/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap index 4d51c727..08115b08 100644 --- a/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap +++ b/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap @@ -3,7 +3,7 @@ exports[`.not.toHaveBeenCalledExactlyOnceWith fails if mock was invoked exactly once with the expected value 1`] = ` "expect(received).not.toHaveBeenCalledExactlyOnceWith() -Expected mock to be invoked some number of times other than once or once with arguments other than ["hello"], but was invoked 1 times with "hello"" +Expected mock to be invoked some number of times other than once or once with arguments other than ["hello"], but was invoked 1 times with ["hello"]" `; exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` @@ -30,11 +30,19 @@ Received has value: [Function mock1]" exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not the expected one 1`] = ` "expect(received).toHaveBeenCalledExactlyOnceWith(expected) -Expected mock function to have been called exactly once with ["hello"], but it was called with "not hello"" +Expected: "hello" +Received: "not hello"" `; exports[`.toHaveBeenCalledExactlyOnceWith fails when one given value is not the expected one 1`] = ` "expect(received).toHaveBeenCalledExactlyOnceWith(expected) -Expected mock function to have been called exactly once with ["hello", "where"], but it was called with "hello"" +- Expected arguments - 1 ++ Received arguments + 1 + + Array [ + "hello", +- "where", ++ "there", + ]" `;