reactjs - React Native Expo - Jest - React Native Firebase - 不变违规 : Native module cannot be null

标签 reactjs react-native jestjs expo react-native-firebase

我尝试 Jest 运行测试,但它因错误而中断:

Invariant Violation: Native module cannot be null.

      at invariant (node_modules/invariant/invariant.js:40:15)
      at RNFBNativeEventEmitter.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:36:7)
      at new RNFBNativeEventEmitter (node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:24:5)
      at Object.<anonymous> (node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:48:16)
      at Object.<anonymous> (node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:21:1)

我对 RN、Expo、Typescript 和其他语言使用最新版本。 我的 Jest 配置是:

"jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|sentry-expo|native-base|@ui-kitten|@react-native-firebase/auth|@react-native-firebase/app)"
    ]
  },

如果我从 transformIgnorePatterns 中删除@react-native-firebase,那么测试会失败并出现错误:

import { isAndroid, isBoolean } from '@react-native-firebase/app/lib/common';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import * as React from 'react';
    > 2 | import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth';

最佳答案

尝试模拟 @react-native-firebase/your/needed/module

例如:

jest.mock('@react-native-firebase/app/lib/common', () => ({
   isAndroid: jest.fn(() => true),
   isBoolean: jest.fn(() => false),
}));

关于reactjs - React Native Expo - Jest - React Native Firebase - 不变违规 : Native module cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720758/

相关文章:

html - 子组件无法正确渲染

javascript - 如何增加 'react-native-swipeout' 上的按钮宽度以进行 ListView 行滑动(React Native)?

react-native - 如何在react-native中获取文件的绝对路径?

jestjs - subscribe() 内的 Expect() 失败不会将测试标记为无效

javascript - 在 vuejs 中测试 firebase 函数

reactjs - Semantic-UI-react:如何使模态后面的背景不可选项

reactjs - React async/await 不在调用函数中等待 - 但被调用函数正在正确执行

javascript - react native ScrollView 不滚动

javascript - MobX : call method of view from action of store 中的事件

angular - 模拟类以使用 Typescript 和 Jest 进行测试