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

Array.toContain() with expect.objectContaining #3954

Closed
KeKs0r opened this issue Jul 1, 2017 · 8 comments
Closed

Array.toContain() with expect.objectContaining #3954

KeKs0r opened this issue Jul 1, 2017 · 8 comments

Comments

@KeKs0r
Copy link

KeKs0r commented Jul 1, 2017

What is the current behavior?
It seems that .toContain() can't be combined with expect.objectContaining. There might be another solution to test if an array of objects contains a specific object, but I somehow thought that combining toContain with expect.objectContaining would do the trick.

Example Test

  test('Array with Object Containing', () => {
    expect(
      [
        { a: 1, b: 2 }, 
        { c: 1, d: 1 }
      ])
      .toContain(
        expect.objectContaining({ a: 1, b: expect.anything() })
      )
  })

Output

 Array with Object Containing

    expect(array).toContain(value)
    
    Expected array:
      [{"a": 1, "b": 2}, {"c": 1, "d": 1}]
    To contain value:
      ObjectContaining {"a": 1, "b": Anything}
@mihir0x69
Copy link

FWIW, check out #2143

@KeKs0r
Copy link
Author

KeKs0r commented Jul 6, 2017

@KarandikarMihir
I think I am missing something. How can I use Object.keys to find a specific object in my array of objects?

@anilreddykatta
Copy link
Contributor

anilreddykatta commented Jul 17, 2017

@KeKs0r If this helps!

test('Array with Object Containing', () => {
  expect(
    [
      { a: 1, b: 2 }, 
      { c: 1, d: 1 }
    ])
    .toContainEqual(
      expect.objectContaining({ a: 1, b: expect.anything() })
    )
})

Is passing without any issues.

@KeKs0r
Copy link
Author

KeKs0r commented Jul 24, 2017

@anilreddykatta thanks this is working fine..

@KeKs0r KeKs0r closed this as completed Jul 24, 2017
@WickyNilliams
Copy link

WickyNilliams commented Oct 24, 2017

I just ran into the same issue. I find it a little confusing that toContain uses strict equality checks, whereas toContainEqual does not. Their names would imply the converse is true?

@justusromijn
Copy link

@WickyNilliams I agree that the naming there is a bit confusing, but you have to think like this: toContain asserts that it contains an exact thing, and in the case of objects, if it isn't the exact same object by reference, it fails. toContainEqual means if it looks the same by shape, it's ok. So it kinda makes sense, but it would have been better to reverse it and have a toContainStrict or something like that.

@WickyNilliams
Copy link

Coming back with more experience of jest, I would say the naming is ok. The toEqual matcher is the equivalent of toContainEqual, in that both test deep equality, not reference equality.

@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants