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

前端 未结 3 788
陌清茗
陌清茗 2021-02-02 04:50

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

I want to inspect the object that is passed as argument.

相关标签:
3条回答
  • 2021-02-02 05:21

    Here is a simple way to assert the parameter passed.

    expect(mockedFunction).toHaveBeenCalledWith("param1","param2");
    
    0 讨论(0)
  • 2021-02-02 05:29

    I prefer lastCalledWith() over toHaveBeenCalledWith(). They are both the same but the former is shorter and help me reduce the cognitive load when reading code.

    expect(mockedFn).lastCalledWith('arg1', 'arg2')
    
    0 讨论(0)
  • 2021-02-02 05:35

    Just use mockObject.calls. In my case I used:

    const call = mockUpload.mock.calls[0][0]
    

    Here's the documentation about the mock property

    0 讨论(0)
提交回复
热议问题