Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect.anything() error #4916

Closed
rrustia opened this issue Nov 19, 2017 · 6 comments · Fixed by #5007
Closed

expect.anything() error #4916

rrustia opened this issue Nov 19, 2017 · 6 comments · Fixed by #5007

Comments

@rrustia
Copy link

rrustia commented Nov 19, 2017

In the expect.anything sample code, it's throwing an error in line 3("[1].map(mock)").

// Test code as below:

test('map calls its argument with a non-null argument', () => {
const mock = jest.fn();
[1].map(mock);
expect(mock).toBeCalledWith(expect.anything());
});

// Output from test below:

expect(jest.fn()).toBeCalledWith(expected)

Expected mock function to have been called with:
  [Anything]
But it was called with:
  [1, 0, [1]]
@rogeliog
Copy link
Contributor

Hi, thanks for reporting!

When you call mock(1) it calls the mock function with one arg, which is why this works...

  mock(1)
  expect(mock).toBeCalledWith(expect.anything());

But calling [1].map(mock); fails because map adds two extra parameters to the mock call(index and array).

To make it work you could do one of the following:

1. Add more arguments to toBeCalledWith

[1].map(mock);
expect(mock).toBeCalledWith(expect.anything(), expect.anything(), expect.anything());

2. Call mock from an anonymous function in the map

[1].map(x => mock(x));
expect(mock).toBeCalledWith(expect.anything());

@thymikee
Copy link
Collaborator

Thanks @rogeliog <3

@SimenB
Copy link
Member

SimenB commented Nov 23, 2017

@thymikee @rogeliog the issue is that the the example in the OP is from the docs.

https://github.com/facebook/jest/blob/2ae45c306bf03612061ce2c802857d74cca7d215/docs/ExpectAPI.md#expectanything

@rrustia would you like to send a PR with one of the fixes above?

@alopezsanchez
Copy link
Contributor

Hello! I just did a PR fixing this small issue 😃

@rrustia
Copy link
Author

rrustia commented Dec 4, 2017 via email

@cpojer cpojer closed this as completed Dec 4, 2017
cpojer pushed a commit that referenced this issue Dec 4, 2017
* docs: update expect.anything() example

Fixes: #4916

* changelog: update with #5007
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants