From d065e87a2322e1b4b70e7ba9de3c24aed7a2ea72 Mon Sep 17 00:00:00 2001 From: jessecarfb <35941444+jessecarfb@users.noreply.github.com> Date: Thu, 15 Feb 2018 16:02:24 +0000 Subject: [PATCH] Issue #5197: Add descriptive error to Expect CalledWith methods when missing optional arguments (#5547) --- CHANGELOG.md | 2 ++ .../__snapshots__/spy_matchers.test.js.snap | 21 +++++++++++++++++++ .../expect/src/__tests__/spy_matchers.test.js | 9 ++++++++ packages/expect/src/spy_matchers.js | 6 ++++++ 4 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2de3a2d8c3c7..2e19d82c71ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ### Fixes +* `[expect]` Add descriptive error message to CalledWith methods when missing + optional arguments ([#5547](https://github.com/facebook/jest/pull/5547)) * `[jest-cli]` Fix inability to quit watch mode while debugger is still attached ([#5029](https://github.com/facebook/jest/pull/5029)) * `[jest-haste-map]` Properly handle platform-specific file deletions diff --git a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap index 65e5ae9dd4bc..484e8f52260e 100644 --- a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -79,6 +79,13 @@ Expected mock function to have been last called with: \\"bar\\" as argument 2, but it was called with \\"bar3\\"." `; +exports[`lastCalledWith works with trailing undefined arguments 1`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + Did not expect argument 2 but it was called with undefined." +`; + exports[`toBeCalled works only on spies or jest.fn 1`] = ` "expect(jest.fn())[.not].toBeCalled() @@ -302,6 +309,13 @@ Expected mock function to have been called with: \\"bar\\" as argument 2, but it was called with \\"bar1\\"." `; +exports[`toHaveBeenCalledWith works with trailing undefined arguments 1`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + Did not expect argument 2 but it was called with undefined." +`; + exports[`toHaveBeenLastCalledWith works only on spies or jest.fn 1`] = ` "expect(jest.fn())[.not].toHaveBeenLastCalledWith() @@ -380,3 +394,10 @@ exports[`toHaveBeenLastCalledWith works with many arguments that don't match 1`] Expected mock function to have been last called with: \\"bar\\" as argument 2, but it was called with \\"bar3\\"." `; + +exports[`toHaveBeenLastCalledWith works with trailing undefined arguments 1`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + Did not expect argument 2 but it was called with undefined." +`; diff --git a/packages/expect/src/__tests__/spy_matchers.test.js b/packages/expect/src/__tests__/spy_matchers.test.js index d7e104c299ca..5e200edd834d 100644 --- a/packages/expect/src/__tests__/spy_matchers.test.js +++ b/packages/expect/src/__tests__/spy_matchers.test.js @@ -164,6 +164,15 @@ describe('toHaveBeenCalledTimes', () => { ).toThrowErrorMatchingSnapshot(); }); + test(`${calledWith} works with trailing undefined arguments`, () => { + const fn = jest.fn(); + fn('foo', undefined); + + expect(() => + jestExpect(fn)[calledWith]('foo'), + ).toThrowErrorMatchingSnapshot(); + }); + test(`${calledWith} works with Map`, () => { const fn = jest.fn(); diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index 8310e0baad7d..aaab97821d89 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -219,8 +219,14 @@ const formatMismatchedArgs = (expected, received) => { ` ${printExpected(expected[i])} as argument ${i + 1}, ` + `but it was called with ${printReceived(received[i])}.`, ); + } else if (i >= expected.length) { + printedArgs.push( + ` Did not expect argument ${i + 1} ` + + `but it was called with ${printReceived(received[i])}.`, + ); } } + return printedArgs.join('\n'); };