React native &引用;“类型错误”;在Jest测试中尝试渲染react本机重新激活的节点时
在尝试以玩笑的方式呈现应用程序以进行一些集成测试时,我遇到以下错误:React native &引用;“类型错误”;在Jest测试中尝试渲染react本机重新激活的节点时,react-native,jestjs,react-native-reanimated,React Native,Jestjs,React Native Reanimated,在尝试以玩笑的方式呈现应用程序以进行一些集成测试时,我遇到以下错误: TypeError: Cannot read property 'createNode' of undefined at AnimatedParam.createNode [as __nativeInitialize] (node_modules/react-native-reanimated/src/core/AnimatedNode.js:126:24) at AnimatedParam.
TypeError: Cannot read property 'createNode' of undefined
at AnimatedParam.createNode [as __nativeInitialize] (node_modules/react-native-reanimated/src/core/AnimatedNode.js:126:24)
at AnimatedParam.__nativeInitialize [as __attach] (node_modules/react-native-reanimated/src/core/AnimatedNode.js:71:10)
at new __attach (node_modules/react-native-reanimated/src/core/AnimatedParam.js:11:10)
at createAnimatedParam (node_modules/react-native-reanimated/src/core/AnimatedParam.js:71:10)
at createAnimatedFunction (node_modules/react-native-reanimated/src/core/AnimatedFunction.js:38:17)
at Object.<anonymous> (node_modules/react-native-reanimated/src/derived/interpolate.js:17:39)
而ReanimatedModule
是react native
库中的NativeModule
的类型别名。除此之外,我还没有找到任何有用的信息来帮助解决这个问题
这里特别奇怪的是,我没有在我的代码库中直接使用react native reanimated
,而且据我所知,我能找到的唯一使用它的库组件没有在正在测试的组件中呈现
我无法以任何合理的方式精简我的代码以复制此问题,并且所讨论的代码受公司版权保护,因此我无法共享存储库。我将继续尝试在一个小例子中重现错误,但我想把这个问题提出来,以防有人有过这个问题的经验。我也遇到了这个问题 我最终需要用如下代码模拟整个复活模块:
__nativeInitialize() {
if (!this.__initialized) {
ReanimatedModule.createNode(this.__nodeID, { ...this.__nodeConfig });
this.__initialized = true;
}
}
jest.mock('react-native-reanimated', () => {
const View = require('react-native').View;
return {
Value: jest.fn(),
event: jest.fn(),
add: jest.fn(),
eq: jest.fn(),
set: jest.fn(),
cond: jest.fn(),
interpolate: jest.fn(),
View: View,
Extrapolate: { CLAMP: jest.fn() },
Transition: {
Together: 'Together',
Out: 'Out',
In: 'In',
},
};
});
我在一个/spec_config/jest.js
文件中有这个文件,该文件(连同一些其他全局模拟)在我的jest.config.js
文件中加载了以下行:setupfileafterenv:['./spec_config/jest.js'],
对我来说似乎是一团糟,但我想这就是这个世界的现状。(这是GitHub的问题:)如果您需要模拟
react native reanimated
,您不必手动操作,因为他们现在在模块本身中提供模拟
简单地将这一行添加到您的jest.setup.js
文件中:
jest.mock('react-native-reanimated',()=>
要求('react-native-reanimated/mock')
);
本PR介绍了这一点:问题在于react native reanimated。按照中的步骤进行操作 . 现在应该可以用了。如果没有,请尝试修复react本机手势处理程序,如中所示