score:6

Accepted answer

Say you're testing for existence via selector as in your example and need to use a method that returns ShallowWrapper, then yes you need to use .length. But you don't necessarily need separate statements for it:

const wrapper = shallow(<MYReactComponent />);
expect(wrapper.find('some-css-selector').length).to.equal(1);

For that matter you could consolidate the whole thing in a case like this if you want:

expect(
  shallow(<MYReactComponent />)
  .find('some-css-selector').length
).to.equal(1);

If you're testing based on component type / props then you may be able to use contains(), which returns boolean:

const wrapper = shallow(<MYReactComponent />);
expect(wrapper.contains(
  <div class="something">
    <span>content</span>
  </div>
)).to.be.true;

score:1

Can skip find by directly using exists

expect(wrapper.exists('some-css-selector')).to.equal(true)

Also works with other React Components nicely

import SomeOtherReactElement from 'path/to/SomeOtherReactElement';

...
...

expect(wrapper.exists(SomeOtherReactElement)).to.equal(true)

score:3

Check out exists():

expect(wrapper.find(MyComponent).exists()).toEqual(true)


Related Query

More Query from same tag