Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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