可以在Jest .toHaveBeenCalledWith()
块中使用expect.stringContaining()
吗?
我目前正在使用:
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_.*/),
});
结果与上面所示相同。
如何在Jest .toHaveBeenCalledWith()
块中使用expect.stringContaining()
?
3条答案
按热度按时间umuewwlo1#
这是一个bug in jest。如果测试中还有其他失败,Jest will show these as failures, even though they would pass,例如:
将(不准确地)显示其他
.toHaveBeenCalledWith()
已失败:mjqavswn2#
是的,应该可以。我写了下面的测试,它通过了:
我唯一能建议的是:
tp5buhyn3#
我刚刚遇到了这个问题,但解决办法很简单: