diff --git a/CHANGELOG.md b/CHANGELOG.md index 81183359d5a7..4e7d7572a09b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +* `[expect]` Improve output format for mismatchedArgs in mock/spy calls. + ([#5846](https://github.com/facebook/jest/pull/5846)) * `[jest-cli]` Add support for using `--coverage` in combination with watch mode, `--onlyChanged`, `--findRelatedTests` and more ([#5601](https://github.com/facebook/jest/pull/5601)) 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 650e63d8826f..8487cb972281 100644 --- a/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -34,7 +34,21 @@ exports[`lastCalledWith works with Map 2`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} as argument 1, but it was called with Map {1 => 2, 2 => 1}." + Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} +as argument 1, but it was called with + Map {1 => 2, 2 => 1}. + +Difference: + +- Expected ++ Received + + Map { +- \\"a\\" => \\"b\\", +- \\"b\\" => \\"a\\", ++ 1 => 2, ++ 2 => 1, + }" `; exports[`lastCalledWith works with Set 1`] = ` @@ -48,14 +62,30 @@ exports[`lastCalledWith works with Set 2`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - Set {3, 4} as argument 1, but it was called with Set {1, 2}." + Set {3, 4} +as argument 1, but it was called with + Set {1, 2}. + +Difference: + +- Expected ++ Received + + Set { +- 3, +- 4, ++ 1, ++ 2, + }" `; exports[`lastCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`lastCalledWith works with arguments that match 1`] = ` @@ -97,28 +127,36 @@ exports[`lastCalledWith works with many arguments that don't match 1`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`lastCalledWith works with many arguments that don't match 2`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`lastCalledWith works with many arguments that don't match 3`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`lastCalledWith works with many arguments that don't match 4`] = ` "expect(jest.fn()).lastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`lastCalledWith works with trailing undefined arguments 1`] = ` @@ -254,7 +292,21 @@ exports[`nthCalledWith works with Map 2`] = ` "expect(jest.fn()).nthCalledWith(expected) Expected mock function first call to have been called with: - Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} as argument 1, but it was called with Map {1 => 2, 2 => 1}." + Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} +as argument 1, but it was called with + Map {1 => 2, 2 => 1}. + +Difference: + +- Expected ++ Received + + Map { +- \\"a\\" => \\"b\\", +- \\"b\\" => \\"a\\", ++ 1 => 2, ++ 2 => 1, + }" `; exports[`nthCalledWith works with Set 1`] = ` @@ -268,14 +320,30 @@ exports[`nthCalledWith works with Set 2`] = ` "expect(jest.fn()).nthCalledWith(expected) Expected mock function first call to have been called with: - Set {3, 4} as argument 1, but it was called with Set {1, 2}." + Set {3, 4} +as argument 1, but it was called with + Set {1, 2}. + +Difference: + +- Expected ++ Received + + Set { +- 3, +- 4, ++ 1, ++ 2, + }" `; exports[`nthCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).nthCalledWith(expected) Expected mock function first call to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`nthCalledWith works with arguments that match 1`] = ` @@ -466,7 +534,21 @@ exports[`toHaveBeenCalledWith works with Map 2`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} as argument 1, but it was called with Map {1 => 2, 2 => 1}." + Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} +as argument 1, but it was called with + Map {1 => 2, 2 => 1}. + +Difference: + +- Expected ++ Received + + Map { +- \\"a\\" => \\"b\\", +- \\"b\\" => \\"a\\", ++ 1 => 2, ++ 2 => 1, + }" `; exports[`toHaveBeenCalledWith works with Set 1`] = ` @@ -480,14 +562,30 @@ exports[`toHaveBeenCalledWith works with Set 2`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - Set {3, 4} as argument 1, but it was called with Set {1, 2}." + Set {3, 4} +as argument 1, but it was called with + Set {1, 2}. + +Difference: + +- Expected ++ Received + + Set { +- 3, +- 4, ++ 1, ++ 2, + }" `; exports[`toHaveBeenCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenCalledWith works with arguments that match 1`] = ` @@ -529,44 +627,68 @@ exports[`toHaveBeenCalledWith works with many arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\". - \\"bar\\" as argument 2, but it was called with \\"bar2\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar2\\". - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenCalledWith works with many arguments that don't match 2`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\". - \\"bar\\" as argument 2, but it was called with \\"bar2\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar2\\". - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenCalledWith works with many arguments that don't match 3`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\". - \\"bar\\" as argument 2, but it was called with \\"bar2\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar2\\". - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenCalledWith works with many arguments that don't match 4`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\". - \\"bar\\" as argument 2, but it was called with \\"bar2\\". + \\"bar\\" +as argument 2, but it was called with + \\"bar2\\". - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenCalledWith works with trailing undefined arguments 1`] = ` @@ -610,7 +732,21 @@ exports[`toHaveBeenLastCalledWith works with Map 2`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} as argument 1, but it was called with Map {1 => 2, 2 => 1}." + Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"} +as argument 1, but it was called with + Map {1 => 2, 2 => 1}. + +Difference: + +- Expected ++ Received + + Map { +- \\"a\\" => \\"b\\", +- \\"b\\" => \\"a\\", ++ 1 => 2, ++ 2 => 1, + }" `; exports[`toHaveBeenLastCalledWith works with Set 1`] = ` @@ -624,14 +760,30 @@ exports[`toHaveBeenLastCalledWith works with Set 2`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - Set {3, 4} as argument 1, but it was called with Set {1, 2}." + Set {3, 4} +as argument 1, but it was called with + Set {1, 2}. + +Difference: + +- Expected ++ Received + + Set { +- 3, +- 4, ++ 1, ++ 2, + }" `; exports[`toHaveBeenLastCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar1\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar1\\"." `; exports[`toHaveBeenLastCalledWith works with arguments that match 1`] = ` @@ -673,28 +825,36 @@ exports[`toHaveBeenLastCalledWith works with many arguments that don't match 1`] "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`toHaveBeenLastCalledWith works with many arguments that don't match 2`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`toHaveBeenLastCalledWith works with many arguments that don't match 3`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`toHaveBeenLastCalledWith works with many arguments that don't match 4`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) Expected mock function to have been last called with: - \\"bar\\" as argument 2, but it was called with \\"bar3\\"." + \\"bar\\" +as argument 2, but it was called with + \\"bar3\\"." `; exports[`toHaveBeenLastCalledWith works with trailing undefined arguments 1`] = ` diff --git a/packages/expect/src/spy_matchers.js b/packages/expect/src/spy_matchers.js index 962febe48ac5..f3fc7d817e67 100644 --- a/packages/expect/src/spy_matchers.js +++ b/packages/expect/src/spy_matchers.js @@ -24,6 +24,7 @@ import { } from 'jest-matcher-utils'; import {equals} from './jasmine_utils'; import {iterableEquality, partition} from './utils'; +import diff from 'jest-diff'; const createToBeCalledMatcher = matcherName => (received, expected) => { ensureNoExpected(expected, matcherName); @@ -268,9 +269,12 @@ const formatMismatchedArgs = (expected, received) => { const printedArgs = []; for (let i = 0; i < length; i++) { if (!equals(expected[i], received[i], [iterableEquality])) { + const diffString = diff(expected[i], received[i]); printedArgs.push( - ` ${printExpected(expected[i])} as argument ${i + 1}, ` + - `but it was called with ${printReceived(received[i])}.`, + ` ${printExpected(expected[i])}\n` + + `as argument ${i + 1}, but it was called with\n` + + ` ${printReceived(received[i])}.` + + (diffString ? `\n\nDifference:\n\n${diffString}` : ''), ); } else if (i >= expected.length) { printedArgs.push(