unit-testing - 基本 react 渲染测试: exists() vs.长度

标签 unit-testing reactjs enzyme

在 React 组件的基本渲染测试中,哪种方法更可靠?

const wrapper = shallow(<MyComponent />);

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

测试渲染的子组件也是如此:

const wrapper = shallow(<MyComponent />);

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

我发现 exists() 更加惯用。但我发现大多数人在测试中使用 length 版本。如果有的话,有哪些权衡?

最佳答案

exists() 只是一个糖 wrapper长度。

 exists() {
    return this.length > 0;
 }

所以使用哪一个取决于你,像你一样,我认为 exists() 更具描述性,更喜欢使用它。

关于unit-testing - 基本 react 渲染测试: exists() vs.长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888943/

相关文章:

python - 修补猎鹰 Hook

java - ApplicationContext 始终为空

javascript - 将 jQuery UI Timepicker 插件与 React 结合使用

javascript - react-custom-scrollbars - 如何更改水平滚动条的颜色?

javascript - React/Enzyme 测试 HTML 标签的值(value)?

javascript - 在 React 组件之外测试 mousedown 事件 : Jest + Enzyme

java - Mockito 通过泛型类型的构造函数参数反射创建模拟

c++ - 如何在 gmock 中测试失败

reactjs - 如何为reactjs添加元素作为树中的标题

reactjs - 使用React Component的代理时查找失败