最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use `expect.stringContaining()` inside a Jest `.toHaveBeenCalledWith()` block? - Stack Overflow

programmeradmin1浏览0评论

Is it possible to use expect.stringContaining() inside a Jest .toHaveBeenCalledWith() block?

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?

Is it possible to use expect.stringContaining() inside a Jest .toHaveBeenCalledWith() block?

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?

Share Improve this question edited Apr 30, 2020 at 20:15 skyboyer 23.7k7 gold badges61 silver badges71 bronze badges asked Apr 30, 2020 at 19:41 mikemaccanamikemaccana 123k110 gold badges427 silver badges531 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

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:

  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:

    - 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,
  • If you're not using the latest version of Jest, try updating. I'm using version 25.5.2, the latest available at the time of writing.

I just ran into this problem, but the solution is quite simple:

expect(createChatRoomMock).toHaveBeenCalledWith(
  expect.objectContaining({
    creatorId: expect.stringContaining("user_"),
    chatRoomUuid: expect.stringContaining("chatRoom_"),
  })
);
发布评论

评论列表(0)

  1. 暂无评论