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 · 7 comments

Comments

@KeKs0r
Copy link

@KeKs0r 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}
@KarandikarMihir

This comment has been minimized.

Copy link

@KarandikarMihir KarandikarMihir commented Jul 6, 2017

FWIW, check out #2143

@KeKs0r

This comment has been minimized.

Copy link
Author

@KeKs0r 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

This comment has been minimized.

Copy link
Contributor

@anilreddykatta 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

This comment has been minimized.

Copy link
Author

@KeKs0r KeKs0r commented Jul 24, 2017

@anilreddykatta thanks this is working fine..

@KeKs0r KeKs0r closed this Jul 24, 2017
@WickyNilliams

This comment has been minimized.

Copy link

@WickyNilliams 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

This comment has been minimized.

Copy link

@justusromijn justusromijn commented Feb 8, 2019

@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

This comment has been minimized.

Copy link

@WickyNilliams WickyNilliams commented Feb 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.