gpt4 book ai didi

javascript - 我可以在 Jest `expect.stringContaining()` block 中使用 `.toHaveBeenCalledWith()` 吗?

转载 作者:行者123 更新时间:2023-12-03 07:05:06 26 4
gpt4 key购买 nike

是否可以使用expect.stringContaining()在 Jest 里面 .toHaveBeenCalledWith()堵塞?

我目前正在使用:

expect(createChatRoomMock).toHaveBeenCalledWith({
creatorId: expect.stringContaining("user_"),
chatRoomUuid: expect.stringContaining("chatRoom_"),
});

但这失败了:

- Expected
+ Received


Object {
- "chatRoomUuid": StringContaining "chatRoom_",
- "creatorId": StringContaining "user_",
+ "chatRoomUuid": "chatRoom_sZ9nj4hC46e4bGz4PjYzpC",
+ "creatorId": "user_nCQsasvYirUwwoEr3j8HsC",
},

这很奇怪,正如您从错误中看到的那样,收到的字符串与预期的匹配

我也试过:
expect(createChatRoomMock).toHaveBeenCalledWith({
creatorId: expect.stringMatching(/user_.*/),
chatRoomUuid: expect.stringMatching(/chatRoom_.*/),
});

与上面显示的结果相同。

如何使用 expect.stringContaining()在 Jest 里面 .toHaveBeenCalledWith()堵塞?

最佳答案

这是 bug in jest .如果测试中还有其他问题,Jest will show these as failures, even though they would pass , 例如:

  it.only("Test", () => {
var createChatRoomMock = jest.fn();

createChatRoomMock({
"chatRoomUuid": "chatRoom_sZ9nj4hC46e4bGz4PjYzpC",
"creatorId": "user_nCQsasvYirUwwoEr3j8HsC",
"somethingElse": "bad"
});

expect(createChatRoomMock).toHaveBeenCalledWith({
creatorId: expect.stringContaining("user_"),
chatRoomUuid: expect.stringContaining("chatRoom_"),
somethingElse: expect.stringContaining("good")
});
});

将(不准确地)显示另一个 .toHaveBeenCalledWith()失败了:
    - Expected
+ Received

Object {
- "chatRoomUuid": StringContaining "chatRoom_",
- "creatorId": StringContaining "user_",
- "somethingElse": StringContaining "good",
+ "chatRoomUuid": "chatRoom_sZ9nj4hC46e4bGz4PjYzpC",
+ "creatorId": "user_nCQsasvYirUwwoEr3j8HsC",
+ "somethingElse": "bad",
},

关于javascript - 我可以在 Jest `expect.stringContaining()` block 中使用 `.toHaveBeenCalledWith()` 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61531444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com