Skip to content

Commit

Permalink
Issue #5197: Add descriptive error to Expect CalledWith methods when …
Browse files Browse the repository at this point in the history
…missing optional arguments (#5547)
  • Loading branch information
jessecarfb authored and mjesun committed Feb 15, 2018
1 parent fcdf071 commit d065e87
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ Expected mock function to have been last called with:
<green>\\"bar\\"</> as argument 2, but it was called with <red>\\"bar3\\"</>."
`;

exports[`lastCalledWith works with trailing undefined arguments 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).lastCalledWith(</><green>expected</><dim>)</>

Expected mock function to have been last called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`toBeCalled works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toBeCalled(</><dim>)</>

Expand Down Expand Up @@ -302,6 +309,13 @@ Expected mock function to have been called with:
<green>\\"bar\\"</> as argument 2, but it was called with <red>\\"bar1\\"</>."
`;

exports[`toHaveBeenCalledWith works with trailing undefined arguments 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).toHaveBeenCalledWith(</><green>expected</><dim>)</>

Expected mock function to have been called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;

exports[`toHaveBeenLastCalledWith works only on spies or jest.fn 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>)[.not].toHaveBeenLastCalledWith(</><dim>)</>

Expand Down Expand Up @@ -380,3 +394,10 @@ exports[`toHaveBeenLastCalledWith works with many arguments that don't match 1`]
Expected mock function to have been last called with:
<green>\\"bar\\"</> as argument 2, but it was called with <red>\\"bar3\\"</>."
`;

exports[`toHaveBeenLastCalledWith works with trailing undefined arguments 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).toHaveBeenLastCalledWith(</><green>expected</><dim>)</>

Expected mock function to have been last called with:
Did not expect argument 2 but it was called with <red>undefined</>."
`;
9 changes: 9 additions & 0 deletions packages/expect/src/__tests__/spy_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};

Expand Down

0 comments on commit d065e87

Please sign in to comment.