如何通过使用toHaveBeenCalledWith仅检查URL?

时间:2020-04-08 19:54:12

标签: reactjs jestjs

我正在测试API,并想检查是否使用了正确的api URL。现在看来,我可以使用toHaveBeenCalledWith了。但是,它将返回该请求选项中的所有内容:

Expected: "http://localhost:5000/myUrls"
Received: "http://localhost:5000/myUrl", {"headers": {"Authorization": "Bearer foobar", "Content-Type": "application/json", "X-Request-ID": "15863752509339714997334447706"}}

仅检查url的常见做法是什么?

1 个答案:

答案 0 :(得分:2)

您可以将第二个参数指定为任意值:

expect(fn).toHaveBeenCalledWith(
    "http://localhost:5000/myUrls",
    expect.anything()
);

查看此处:https://jestjs.io/docs/en/expect#expectanything