Basic react rendering test: exists() vs. length

Viewed 1532

In your basic rendering tests of a React component, which approach is more solid?

const wrapper = shallow(<MyComponent />);

expect(wrapper.exists()).to.equal(true);
expect(wrapper.find('div').length).to.equal(1);

Same thing for testing rendered child components:

const wrapper = shallow(<MyComponent />);

expect(wrapper.find(MyChildComponent).exists()).to.equal(true);
expect(wrapper.find(MyChildComponent).length).to.equal(1);

I find exists() to be much more idiomatic. But I see that most people use the length version in their tests. What are the trade-offs, if any ?

2 Answers
Related