diff --git a/CHANGELOG.md b/CHANGELOG.md index f188240e5201..fb62215e789e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +* `[expect]` toHaveBeenNthCalledWith/nthCalledWith gives wrong call messages if not matched ([#6340](https://github.com/facebook/jest/pull/6340)) * `[jest-each]` Make sure invalid arguments to `each` points back to the user's code ([#6347](https://github.com/facebook/jest/pull/6347)) * `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313)) * `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) 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 7934e2030ac5..95a15e70ccf4 100644 --- a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -261,7 +261,9 @@ exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 1` "expect(jest.fn()).nthCalledWith(expected) Expected mock function first call to have been called with: -" + \\"foo\\" +as argument 1, but it was called with + \\"foo1\\"." `; exports[`nthCalledWith should replace 1st, 2nd, 3rd with first, second, third 2`] = ` @@ -1290,7 +1292,9 @@ exports[`toHaveBeenNthCalledWith should replace 1st, 2nd, 3rd with first, second "expect(jest.fn()).toHaveBeenNthCalledWith(expected) Expected mock function first call to have been called with: -" + \\"foo\\" +as argument 1, but it was called with + \\"foo1\\"." `; exports[`toHaveBeenNthCalledWith should replace 1st, 2nd, 3rd with first, second, third 2`] = ` diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index f84ddb4a8d68..2a176fb9e1f1 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -365,7 +365,11 @@ const createNthCalledWithMatcher = (matcherName: string) => ( `Expected ${identifier} ${nthToString( nth, )} call to have been called with:\n` + - formatMismatchedCalls(calls, expected, LAST_CALL_PRINT_LIMIT); + formatMismatchedCalls( + calls[nth - 1] ? [calls[nth - 1]] : [], + expected, + LAST_CALL_PRINT_LIMIT, + ); return {message, pass}; };