React native 当我开玩笑地用酶检查它时,为什么ScrollView隐藏了它的孩子?

React native 当我开玩笑地用酶检查它时,为什么ScrollView隐藏了它的孩子?,react-native,jestjs,enzyme,expo,React Native,Jestjs,Enzyme,Expo,我有一个React原生项目,正在使用Jest+酶作为测试环境 每当我尝试挂载ScrollView组件时,我似乎无法在组件树中看到它的下方 这是我的测试: describe('Example test', () => { it('should find text', () => { const wrapper = mount(<View><Text>lorem ipsum dolor sit</Text></View>);

我有一个React原生项目,正在使用Jest+酶作为测试环境

每当我尝试挂载ScrollView组件时,我似乎无法在组件树中看到它的下方

这是我的测试:

describe('Example test', () => {
  it('should find text', () => {
    const wrapper = mount(<View><Text>lorem ipsum dolor sit</Text></View>);
    expect(wrapper.find('Text').length).toEqual(1);
  });

  it('should find text in ScrollView', () => {
    const wrapper = mount(<ScrollView><Text>lorem ipsum dolor sit</Text></ScrollView>);
    expect(wrapper.find('Text').length).toEqual(1);
  });
});
description('示例测试',()=>{
它('应该找到文本',()=>{
常量包装=装载(lorem ipsum dolor sit);
expect(wrapper.find('Text').length).toEqual(1);
});
它('应该在ScrollView中找到文本',()=>{
常量包装=装载(lorem ipsum dolor sit);
expect(wrapper.find('Text').length).toEqual(1);
});
});
当我将第一个测试包装在
视图中时,它运行良好。它可以很好地看到
文本

第二个测试失败,因为它在
滚动视图
下没有看到任何
文本
组件

你知道为什么吗

我在这里设置了一个完整的测试示例:


只需按照自述文件中的说明进行操作。

您应该尝试模拟它,检查此项

我尝试了这个代码,它的工作

jest.mock("ScrollView", () => {
   const RealComponent = require.requireActual("ScrollView");
   const React = require("React");
   class ScrollView extends React.Component {
   scrollTo() {}

   render() {
      return React.createElement("ScrollView", this.props, 
   this.props.children);
   }
  }
  ScrollView.propTypes = RealComponent.propTypes;
  return ScrollView;
});

你应该试着模仿它,检查这个

我尝试了这个代码,它的工作

jest.mock("ScrollView", () => {
   const RealComponent = require.requireActual("ScrollView");
   const React = require("React");
   class ScrollView extends React.Component {
   scrollTo() {}

   render() {
      return React.createElement("ScrollView", this.props, 
   this.props.children);
   }
  }
  ScrollView.propTypes = RealComponent.propTypes;
  return ScrollView;
});

是的,我已经有了类似的解决方法。但这似乎不必要,对吧?你知道是什么让这成为必要吗?显然模拟
ScrollView
与其他react本机组件有点不同,这也发生在
ListView
上,我相信,在我提到的问题中,你可能会注意到
SrollViewMock
的PR,这是我们在开玩笑时调用的组件(“滚动视图”,…),据我所知,除此之外,我不认为还有其他解决方案。非常感谢您的解释。我们将采用这一路线。是的,我已经有了类似的解决方法。但似乎没有必要这样做,对吗?知道是什么让这成为必要吗?显然模拟
滚动视图
是必要的与其他react本机组件稍有不同的是,
ListView
也会出现这种情况,我相信,在我提到的问题中,您可能会注意到
SrollViewMock
的PR,它是在我们执行jest.mock(“ScrollView”,…)时被调用的组件,据我所知,除此之外,我认为没有其他解决方案。非常感谢您的解释。我们将沿着这条路线走。