gpt4 book ai didi

reactjs - 在 React Native 中,如何使用浅层渲染测试我的组件?

转载 作者:行者123 更新时间:2023-12-03 13:08:40 26 4
gpt4 key购买 nike

对于React ,我用Shallow Rendering对我的 React 组件进行单元测试的技术。我可以在 React Native 中做类似的事情吗? ?

我已经followed the instructions to set up Jest ,但找不到任何有关测试我的组件的文档。我想使用 React Native 进行完整的 TDD,就像使用 React 一样。

最佳答案

我认为enzyme这就是您正在寻找的。

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

Enzyme 可以与所有流行的测试运行程序(如 Mocha、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);
});
});

要进一步阅读,您可以查看 enzyme 的 Shallow Rendering APIdocs一般来说。

关于reactjs - 在 React Native 中,如何使用浅层渲染测试我的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32804218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com