Skip to content
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

Closed
kennethnym opened this issue Feb 29, 2020 · 4 comments

Comments

@kennethnym
Copy link

🐛 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:

it('should be able to update document in conversations collection', async () => {
  const mockMessageId = mongoose.Types.ObjectId();
  const mockMessage = {
    _id: mockMessageId,
    type: 'TEXT',
    content: 'testMessage',
  };

  await Conversation.findByIdAndUpdate(mockObjectId, {
    $push: { messages: mockMessage },
  });

  const updatedConversation = await Conversation.findById(mockObjectId);

  expect(updatedConversation.messages).toContainEqual(mockMessage);
});

which gives:

Error: expect(received).toContainEqual(expected) // deep equality

Expected value: {"_id": "5e599695fb41595b3093ada6", "content": "testMessage", "type": "TEXT"}
Received array: [{"_id": "5e599695fb41595b3093ada6", "content": "testMessage", "type": "TEXT"}]

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

npx: installed 1 in 1.452s

  System:
    OS: macOS 10.15.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Binaries:
    Node: 10.17.0 - ~/.nvm/versions/node/v10.17.0/bin/node
    Yarn: 1.22.0 - /usr/local/bin/yarn
    npm: 6.11.3 - ~/.nvm/versions/node/v10.17.0/bin/npm
  npmPackages:
    jest: ^25.1.0 => 25.1.0 
@kennethnym
Copy link
Author

kennethnym commented Mar 8, 2020

What I'm doing to avoid this is I reorder the keys and then stringify the objects before comparing. Kinda hacky, but it works.

@rogeliog
Copy link
Contributor

rogeliog commented Mar 9, 2020

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 createdConversation.messages contain more keys than just _id, type, content. createdConversation.messages also contains the following keys __parentArray, __index, $isDocumentArrayElement, $__, isNew, errors, _doc, $locals, $op, $init.

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.objectContaining(...))

-    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 😄

@kennethnym
Copy link
Author

kennethnym commented Mar 9, 2020

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.

@github-actions
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 May 11, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants