对于React,我使用Shallow Rendering技术来对React组件进行单元测试。我可以在React Native中做类似的事情吗?
我已经按照说明设置了Jest,但是找不到有关测试组件的任何文档。我想以与React相同的方式对React Native进行完整的TDD。
我认为enzyme是您想要的。
它提供了一个shallow功能,使您可以根据需要进行浅表比较。
shallow
酶可以与所有流行的测试跑步者一起使用(例如Mocha,Jest,Karma等)。完整列表可以在库的github页面上找到。
例:
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 API或一般文档。