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:
- Created 3 years ago
- Reactions:4
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Feel free to send a PR 🙂
This sounds like what #9257 was supposed to fix… Maybe it’s just missing for this matcher?