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

Unexpected error (without message) of "toHaveBeenLastCalledWith" #5357

Closed
chris-visser opened this issue Jan 21, 2018 · 2 comments
Closed

Unexpected error (without message) of "toHaveBeenLastCalledWith" #5357

chris-visser opened this issue Jan 21, 2018 · 2 comments

Comments

@chris-visser
Copy link

I've stumbled upon an issue where Jest is not clear to me and throw's an invalid error without a message:

Error: expect(jest.fn()).toHaveBeenLastCalledWith(expected)

Expected mock function to have been last called with:


      368 |     it('Should call the storage assignRole method with the right parameters', () => {
      369 |       Privileges.assignRole('chris', 'admin');
    > 370 |       expect(mockStorage.assignRole).toHaveBeenLastCalledWith('chris', 'admin');
      371 |     });
      372 |   });
      373 | 
      
      at Object.it (lib/privileges.test.js:370:38)

After some research I found out that the error is thrown, because i'm pushing a 3th parameter with an undefined value into my jest mock function like shown below:

  // Calling this method with assignRole('chris', 'admin')
  assignRole(userId, role, group) {
    // Below 'assignRole' method call is mocked. group === undefined
    return this.storage.assignRole(userId, role, group);
  }

On the other hand, testing it with explicitly checking for undefined will result in a valid test:

    it('Should call the storage assignRole method with the right parameters', () => {
      Privileges.assignRole('chris', 'admin');
      expect(mockStorage.assignRole).toHaveBeenLastCalledWith('chris', 'admin', undefined);
    });

I would expect Jest not to throw any error when I'm not checking the 3th parameter. If it does need to throw any error, because I am passing the group parameter (explicitly checking all parameters) then I would expect Jest to throw me an error along the lines of "Expected mock function to have been last called with 2 parameters. Instead it was called with 3."

I'm using the latest Jest version "22.1.4".

Thank you!

@SimenB
Copy link
Member

SimenB commented Jan 23, 2018

Duplicate of #5197.

@SimenB SimenB closed this as completed Jan 23, 2018
@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants