Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A single failure in toHaveBeenCalledWith() when using expect.stringContaining() will show other expected results as failed #9937

Closed
mikemaccana opened this issue May 1, 2020 · 12 comments

Comments

@mikemaccana
Copy link
Contributor

mikemaccana commented May 1, 2020

🐛 Bug Report

A single failure in toHaveBeenCalledWith() when using expect.stringContaining() will show other successful results as failed. See the reproduction case below!

To Reproduce

      it.only("Test", () => {
        var createChatRoomMock = jest.fn();
    
        createChatRoomMock({
            "chatRoomUuid": "chatRoom_a",
            "creatorId": "user_b",
            "somethingElse": "bad"
        });
    
        expect(createChatRoomMock).toHaveBeenCalledWith({
          creatorId: expect.stringContaining("user_"),
          chatRoomUuid: expect.stringContaining("chatRoom_"),
          somethingElse: expect.stringContaining("good")
        });
      });

Expected behavior

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      Object {
    -   "somethingElse": StringContaining "good",
    +   "somethingElse": "bad",
      },

Actual behaviour

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

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

Link to repl or repo (highly encouraged)

https://repl.it/repls/ImpishTerrificOperation

envinfo

  System:
    OS: Linux 4.19 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  Binaries:
    Node: 10.20.1 - /usr/bin/node
    npm: 6.14.4 - /usr/bin/npm
  npmPackages:
    jest: ^25.5.3 => 25.5.3
@mikemaccana
Copy link
Contributor Author

Note: originally repl.it failed with:

Oh No! The IDE is having a bit of trouble.

So there wasn't a repl link. It's been added now, so the Needs Repro badge can be removed.

@SimenB
Copy link
Member

SimenB commented May 2, 2020

This sounds like what #9257 was supposed to fix... Maybe it's just missing for this matcher?

@julestruong
Copy link

any news on this ? i'm on 27.0.6

@SimenB
Copy link
Member

SimenB commented Feb 7, 2022

Feel free to send a PR 🙂

@julestruong
Copy link

@SimenB Since its been 2 years, i was just asking some news if this is been fixed ?

@SimenB
Copy link
Member

SimenB commented Feb 7, 2022

You could try out using the latest version of Jest yourself. Seeing as you say it still reproduces on 27.0.6 it seems unlikely to have been fixed

@mrazauskas
Copy link
Contributor

mrazauskas commented Feb 7, 2022

Checked it with the latest version. Seems to still be there. Did not fix itself (;

@julestruong
Copy link

i used a workaround by making a custom matcher , extending it in jest. Sorry i don't have much time to spend to help you guyz.

@JHIH-LEI
Copy link

JHIH-LEI commented Nov 17, 2022

I want to test socket.io its io.to(room).emit(event, data) if had been call and check its args , so I do this way:

my mock:

// ../server is where I export io 
jest.mock("../server", () => {
  
  return {
    redis: new Redis(process.env.REDIS_URL!),
    io: {
      to: jest.fn().mockImplementation((room: string) => {
        return {
          emit: jest
            .fn()
            .mockImplementation((event: string, data: NotifyPopupContent) => {
              console.log(`emit to ${room} at event :${event} success!`);
            }),
        };
      }),
    },
  };
});

my test:
it's just work fine with to: jest.fn , but not ok with to().emit:

expect(io.to).toHaveBeenCalledWith(user2ToBeNotifySocketId) // it pass
const mockEmit = io.to(user2ToBeNotifySocketId).emit as jest.Mock;
expect(mockEmit).toHaveBeenCalledWith("notify", notificationContent); // it get error

error:

expect(jest.fn()).toHaveBeenCalledWith(...expected)
 Expected: "notify", {"avatar": "avatar", "createdAt": "2022-11-17T14:07:07.353Z", "id": "6376400baa977167871a8c8a", "main": "newTweet", "name": "user1", "type": "tweet", "userId": "6376400baa977167871a8c89"}

That's weird.
It should can work but not...
Any Idea?

Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 17, 2023
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2023
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants