Jest: 对于包含以下内容的组件,React Native 快照失败并显示“TypeError:无法读取未定义的属性‘_tag’”<textinput/>

创建于 2016-08-06  ·  3评论  ·  资料来源: facebook/jest

重现:

  1. 修改https://github.com/facebook/jest/blob/master/examples/react-native/Intro.js在 render() 中添加一个 <TextInput />
  2. npm 测试

结果:

失败测试/Intro-test.js (0.459s)
● 介绍 › 它正确呈现

  • 类型错误:无法读取未定义的属性“_tag”
    在 ReactNativeBaseComponent.Mixin.mountComponent (node_modules/react/lib/ReactNativeBaseComponent.js:179:45)
    在 Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    在 ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:397:34)
    在 ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:262:21)
    在 Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    在 ReactCompositeComponentMixin.performInitialMount (node_modules/react/lib/ReactCompositeComponent.js:397:34)
    在 ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:262:21)
    在 Object.ReactReconciler.mountComponent (node_modules/react/lib/ReactReconciler.js:47:35)
    在 ReactMultiChild.Mixin.mountChildren (node_modules/react/lib/ReactMultiChild.js:242:44)
    在 ReactTestComponent.mountComponent (node_modules/react/lib/ReactTestRenderer.js:53:8)

最有用的评论

根据https://facebook.github.io/jest/docs/tutorial-react-native.html#mock -native-modules-using-jest-mock,解决方法是添加

jest.mock('TextInput', () => 'TextInput');

到考试开始。

所有3条评论

根据https://facebook.github.io/jest/docs/tutorial-react-native.html#mock -native-modules-using-jest-mock,解决方法是添加

jest.mock('TextInput', () => 'TextInput');

到考试开始。

我们应该为 TextInput 提供一个默认的模拟。

此页面是否有帮助?
0 / 5 - 0 等级