-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
toContainEqual failing when searching through an array in a mongoose/mongodb object #9624
Comments
What I'm doing to avoid this is I reorder the keys and then stringify the objects before comparing. Kinda hacky, but it works. |
Thanks for reporting and for providing a repo. I've been looking at it, and it seems that the reason it is failing is because the items in You can see the error by changing the expectation to: expect(createdConversation.messages[0]).toEqual({
_id: messageId,
type: "TEXT",
content: "test message"
}); To solve this, you can use this asymmetrical matcher ( - expect(createdConversation.messages).toContainEqual({
- _id: messageId,
- type: 'TEXT',
- content: 'test message',
- });
+ expect(createdConversation.messages).toContainEqual(
+ expect.objectContaining({
+ _id: messageId,
+ type: "TEXT",
+ content: "test message"
+ })
+ ); Let me know if that seems to work for you and I can close the issue if needed 😄 |
Thank you so much for the detailed explanation! I'll look into it. Edit: It works perfectly! Thank you again for helping me out. Closing this issue. |
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. |
🐛 Bug Report
toContainEqual
fails when I try to test whether an object is in an array that is in a mongoose/mongodb object, even though the object is exactly the same as the one in the array.To Reproduce
This is my test snippet:
which gives:
Expected behavior
toContainEqual
should be able to match the object in the array and passes the test.Link to repl or repo (highly encouraged)
https://github.com/MrCreeper1008/jest-tocontainequal-issue
envinfo
The text was updated successfully, but these errors were encountered: