The Dead Man
The Dead Man

Reputation: 5566

Jest: How can I get the arguments called in jest mock function?

I am learning manual mocking with jest mocks, now I would like to get arguments called in jest mock function, here is my solution.

 expect(mockLogin).toHaveBeenCalledWith(
      {
        "email": "[email protected]", 
        "password": "password", 
        "remember_me": false
       }
    );

Now when I run npm test I get the following results. enter image description here

The problem I can't figure out how to add "auth/login" path in my expected results, I tried the following, but unfortunately it's not working.

   expect(mockLogin).toHaveBeenCalledWith("/auth/login", {
        "email": "[email protected]", 
        "password": "password", 
        "remember_me": false
       });

What do I need to do to add auth/login to my expected results?

Upvotes: 3

Views: 9653

Answers (3)

bensiu
bensiu

Reputation: 25584

expect(mockLogin).toHaveBeenCalledWith(['/auth/login', {
  "email": "[email protected]",
  "password": "password", 
  "remember_me": false
}]);

toHaveBeenCalledWith would expect array of arguments.

Upvotes: 0

Jason Dent
Jason Dent

Reputation: 741

There must be something else going on.

Here is a simple example that works:

describe('example', () => {
    test('mock called with', () => {
        const mock = jest.fn();
        const signin = {
            "email": "[email protected]",
            "password": "password",
            "remember_me": false
        };
        mock('/auth/login', signin);

        expect(mock).toHaveBeenCalledWith('/auth/login', {
            "email": "[email protected]",
            "password": "password",
            "remember_me": false
           });
    });
});

Upvotes: 3

lissettdm
lissettdm

Reputation: 13078

You can try with mockFn.mock.calls

expect(mockLogin.mocks.call[0][0]).toEqual("/auth/login");
expect(mockLogin.mocks.call[0][1]).toEqual({
  "email": "[email protected]", 
  "password": "password", 
  "remember_me": false
});

Upvotes: 1

Related Questions