question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Feb 7, 2022

Feel free to send a PR 🙂

1reaction
SimenBcommented, May 2, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Can I use `expect.stringContaining()` inside a Jest ...
stringContaining ("good") }); });. Will (inaccurately) show that the other .toHaveBeenCalledWith() have failed: - Expected + Received Object ...
Read more >
Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >
Jest Expect - w3resource
yourMatcher() fails. We call matchers with the argument passed to expect(x) followed by the arguments passed to .yourMatcher(y, z):
Read more >
Expect · Jest
And when pass is true, message should return the error message for when expect(x).not.yourMatcher() fails. These helper functions can be found on this...
Read more >
Using Jest toHaveBeenCalledWith for testing primitive data ...
Another way to do it can be to only check part of the string like expect.stringContaining('openlibrary.org'); expects the parameter to contain ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found