2015-09-27 104 views
19

对于React,我使用Shallow Rendering技术来对我的React组件进行单元测试。我可以在React Native做类似的事吗?在React Native中,如何使用浅渲染测试我的组件?

我已经followed the instructions to set up Jest,但找不到关于测试我的组件的任何文档。我想用React完成与React Native完全一样的TDD。

+2

好问题。官方文件是非常简短的说至少.. –

+1

https://medium.com/@jcfrancisco/unit-testing-react-native-components-a-firsthand-guide-cea561df242b#.qeg60edil –

+0

至于我' m知道react-native对iOS或Android环境并没有太多的假设。你不能只使用Jest并假装它是一个网络应用程序?是不是所有的依赖都会被嘲笑? – Parris

回答

9

我认为enzyme是你正在寻找。

它为您提供shallow函数,它允许您进行浅层比较(如您所愿)。

酶可以与所有流行的测试运动员(如摩卡,Jest,Karma等)一起使用。完整列表可以发现on the library's github page

例子:

import {shallow} from 'enzyme'; 

describe('<MyComponent />',() => { 
    it('should render three <Foo /> components',() => { 
    const wrapper = shallow(<MyComponent />); 
    expect(wrapper.find(Foo)).to.have.length(3); 
    }); 
}); 

对于进一步阅读,你可以采取酶的Shallow Rendering APIdocs一般看看。