我正在尝试模拟带有 react-native(不是第三方模块(的模块,例如LayoutAnimation
:
import * as RN from 'react-native'
RN.LayoutAnimation = jest.fn()
但测试失败,并显示:
TypeError: Cannot read property 'decelerationRate' of undefined
at Object.<anonymous> (node_modules/react-native/Libraries/Components/WebView/WebView.ios.js:555:3254)
at Object.get WebView [as WebView] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:73:22)
有没有其他方法可以模拟/存根 RN 模块,例如LayoutAnimation
或任何其他 反应原生(非第三方(模块?
尝试简单地做jest.mock('LayoutAnimation');
您收到此消息是因为/node_modules/react-native/Libraries/Components/WebView/WebView.ios 中的第 217 行.js
decelerationRate: ScrollView.propTypes.decelerationRate
因为 ScrollView被模拟 ScrollView.propType === 未定义
我通过添加以下内容解决了这个问题:
import {PropTypes} from 'react';
ScrollView.propTypes = { decelerationRate: PropTypes.number };
设置脚本文件(由package.json的jest部分中的setupTestFrameworkScriptFile属性设置的文件(;