Enzyme: Breaking behavior for `contains` in 2.3.0

Created on 13 May 2016  路  5Comments  路  Source: enzymejs/enzyme

Hey all,

I'm using enzyme with React Native (0.25). In 2.2.0 this was a passing test, but in 2.3.0 it fails citing that MessageHeader isn't a valid React element (Error: ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.).

import MessageHeader from '../../src/components/MessageHeader'

it('renders the broadcast header', () => {
  expect(wrapper.contains(MessageHeader), 'to be truthy')
})

This appears to be from the changes in https://github.com/airbnb/enzyme/commit/c80249a24bc1ceefe192b502c36a1e904133aef7.

I'm unsure if this is the intended behavior or a bug. wrapper.find(MessageHeader) passes, but I feel like I shouldn't be making api changes to the test on a minor version update.

Thoughts?

invalid

Most helpful comment

I guess the new containsMatchingElement method might also work for this, no?

All 5 comments

The commit message indicates that your previous passing test was a false positive - so this was considered a bugfix. It's tricky for a testing framework to ship false positive bugfixes - conceptually it's both a patch and a major version - but in this case, I think you knowing that your test wasn't previously testing anything is important enough to warrant a patch.

Yeah that makes sense. So what's the right way to do this? I thought wrapper.contains(<MessageHeader />), but that also fails. Maybe I'm misunderstanding how shallow works?

expect(wrapper.find(MessageHeader)).to.have.lengthOf(1) for example. wrapper.contains accepts a ReactElement that _exactly matches_ the one in the tree, props and all.

I guess the new containsMatchingElement method might also work for this, no?

Looks like containsMatchingElement is the behavior I'm looking for.

Thanks @ljharb for clarifying the behavior and @nfcampos for the tip :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abe903 picture abe903  路  3Comments

timhonders picture timhonders  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

mattkauffman23 picture mattkauffman23  路  3Comments

rexonms picture rexonms  路  3Comments