简体   繁体   English

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

[英]Can I use `expect.stringContaining()` inside a Jest `.toHaveBeenCalledWith()` block?

Is it possible to use expect.stringContaining() inside a Jest .toHaveBeenCalledWith() block?是否可以在 Jest .toHaveBeenCalledWith()块中使用expect.stringContaining()

I am currently using:我目前正在使用:

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

But this fails with:但这失败了:


    - Expected
    + Received


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

This is odd, as you can see from the error, the recieved strings match what's expected这很奇怪,正如您从错误中看到的那样,收到的字符串与预期的匹配

I've also tried:我也试过:

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

With the same results as shown above.与上面显示的结果相同。

How can I use expect.stringContaining() inside a Jest .toHaveBeenCalledWith() block?如何在 Jest .toHaveBeenCalledWith()块中使用expect.stringContaining() )?

This is a bug in jest .这是开玩笑的错误 If there is anything else failing in the test, Jest will show these as failures, even though they would pass , for example:如果测试中有任何其他失败, Jest 会将这些显示为失败,即使它们会通过,例如:

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

Will (inaccurately) show that the other .toHaveBeenCalledWith() have failed:将(不准确地)显示另一个.toHaveBeenCalledWith()失败:

    - Expected
    + Received

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

Yes, it should be possible.是的,这应该是可能的。 I wrote the following test and it passed:我编写了以下测试并通过了:

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

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

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

The only things I could suggest are:我唯一能建议的是:

  • Check for hidden characters such as Unicode zero-width spaces in the output,检查 output 中的 Unicode 零宽度空格等隐藏字符,
  • If you're not using the latest version of Jest, try updating.如果您使用的不是最新版本的 Jest,请尝试更新。 I'm using version 25.5.2, the latest available at the time of writing.我正在使用 25.5.2 版本,这是撰写本文时可用的最新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM