最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest: How can I get the arguments called in jest mock function? - Stack Overflow

programmeradmin4浏览0评论

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.

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?

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.

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?

Share Improve this question edited Nov 26, 2020 at 16:58 The Dead Man asked Nov 24, 2020 at 13:15 The Dead ManThe Dead Man 5,57633 gold badges125 silver badges226 bronze badges 1
  • 1 Could you post the code of your test? And maybe additionally the code you're testing. – amakhrov Commented Dec 3, 2020 at 4:55
Add a ment  | 

3 Answers 3

Reset to default 3 +25

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
           });
    });
});

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
});
expect(mockLogin).toHaveBeenCalledWith(['/auth/login', {
  "email": "[email protected]",
  "password": "password", 
  "remember_me": false
}]);

toHaveBeenCalledWith would expect array of arguments.

发布评论

评论列表(0)

  1. 暂无评论