我正在使用Jest和react-native-testing-library来测试组件。
在我的组件之一中,我有以下代码:
const handleToggleFilters = () => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
setPostFiltersActive(!postFiltersActive);
};
但是,在测试组件时,出现以下错误
TypeError: require(...).configureNextLayoutAnimation is not a function
82 |
83 | const handleToggleFilters = () => {
> 84 | LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
| ^
85 | setPostFiltersActive(!postFiltersActive);
86 | };
87 |
我在我的setup.js文件中添加了jest.mock('react-native')
,但随后在我的测试套件的其余部分中,它开始抱怨其他缺少的实体...我是否必须为此模拟整个react-native
库工作?
解决此问题的最佳方法是什么?
答案 0 :(得分:2)
在github上查看来自react-native
的某些测试后,看起来他们自己简单地模拟了文件。
// Libraries/Components/Keyboard/__tests__/Keyboard-test.js
jest.mock('../../../LayoutAnimation/LayoutAnimation');
因此在我的setup.js
文件中,我只是做了
jest.mock(
'../node_modules/react-native/Libraries/LayoutAnimation/LayoutAnimation.js',
);
我的测试通过了。