Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 543
mikemaccana Avatar asked Apr 30 '20 19:04

mikemaccana


2 Answers

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",
      },
like image 187
mikemaccana Avatar answered Sep 30 '22 03:09

mikemaccana


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.
like image 44
Luke Woodward Avatar answered Sep 30 '22 03:09

Luke Woodward