我正在使用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 |我将jest.mock('react-native')添加到我的setup.js文件中,但随后它开始抱怨我的测试套件的其余部分中缺少其他实体……我必须模拟整个react-native库才能正常工作吗?
解决这个问题的最好方法是什么?
发布于 2020-01-24 20:26:08
在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',
);我的测试也通过了。
https://stackoverflow.com/questions/59896265
复制相似问题