react-native - ReactNative 0.48 上的开 Jest 错误 "Cannot read property ' validAttributes' of undefined"

标签 react-native jestjs

我们最近将一个应用程序从 ReactNative 0.42 更新到了 0.48。在那次更新中,我们迁移到 Jest 进行测试(来自 mocha/chai/enzyme)。我们目前正在使用 Jest v21.1.0。当我手动运行每个测试时,它们都无误地通过了。当我只运行 yarn jest 时,我得到这个错误:

/scratch/react_native_app/client/node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:2582
warnForStyleProps$1(nativeProps,viewConfig.validAttributes);
                                          ^

TypeError: Cannot read property 'validAttributes' of undefined
    at setNativePropsStack$1 (/scratch/react_native_app/client/node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:2582:43)
    at Component.setNativeProps (/scratch/react_native_app/client/node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:2550:1)
    at AnimatedProps.callback [as _callback] (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:1819:20)
    at AnimatedProps.update (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:1698:6)
    at /scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:230:69
    at Set.forEach (native)
    at _flush (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:230:16)
    at AnimatedValue._updateValue (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:939:1)
    at TimingAnimation.animation.start._this9._animation [as _onUpdate] (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:906:8)
    at TimingAnimation.onUpdate (/scratch/react_native_app/client/node_modules/react-native/Libraries/Animated/src/AnimatedImplementation.js:345:6)

我不能确定,但​​我们只有两个处理动画和计时的组件。我们正在使用 jest.useFakeTimers(); 如果我添加一个 jest.runAllTimers(); 我可以在单个组件上得到错误。组件的计时部分如下所示:

  componentDidMount() {
      Animated.timing(
        this.state.fadeAnim, {
          toValue: 1,
          delay: 2000
        }
      ).start();
  }

<Animated.View style={{ opacity: this.state.fadeAnim }}>
 ...
</Animated.View>

最佳答案

我花了很长时间,但我终于弄明白是怎么回事了。基本上,我想的更多的测试是渲染动画 View 。我最初将 jest.useFakeTimers(); 包含在我对该组件的特定测试中,但后来我将其移至名为 test/jest_setup.js 的设置文件中,然后添加这是我的 package.json 文件:

  "jest": {
    "setupFiles": [
      "./node_modules/react-native/jest/setup.js",
      "./test/jest_setup.js"
    ],
    // rest of jest config
  }

现在我的错误消失了!

关于react-native - ReactNative 0.48 上的开 Jest 错误 "Cannot read property ' validAttributes' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432694/

相关文章:

android - 当应用程序处于后台状态 React native 时深度链接不起作用

javascript - 如何获得用户偏好的单位?米或英里

javascript - 从 javascript 使用 CapsLock 触发新的键盘事件

javascript - JestJS 测试在 request-promise-core 内部失败

reactjs - 自动预测在 iOS 13 中无法使用 textContentType 到 TextInput

android - react native :how to understand device is rooted?

android - 如何更改 React Native 和 Metro Bundler 端口

javascript - 在 Jest 测试中等待 Promise.all

reactjs - 用 Jest 模拟 react-beautiful-dnd

javascript - Jest getReversedNavigationSections 为 null