React Native Expo - Jest - React Native Firebase - Invariant Violation:Native 模块不能为空
Posted
技术标签:
【中文标题】React Native Expo - Jest - React Native Firebase - Invariant Violation:Native 模块不能为空【英文标题】:React Native Expo - Jest - React Native Firebase - Invariant Violation: Native module cannot be null 【发布时间】:2020-04-30 09:51:14 【问题描述】:我尝试用 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":
"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';
【问题讨论】:
我有同样的错误,你找到解决办法了吗?谢谢 我没有。对不起。 【参考方案1】:尝试模拟@react-native-firebase/your/needed/module
例如:
jest.mock('@react-native-firebase/app/lib/common', () => (
isAndroid: jest.fn(() => true),
isBoolean: jest.fn(() => false),
));
【讨论】:
您是否尝试模拟其他模块(auth 和 FirebaseAuthTypes)? 虽然我使用的是'@react-native-firebase/ml-vision',但这个建议让我找到了一个解决方案,正如建议的那样,它是模拟依赖项。就我而言,我做了: jest.mock('@react-native-firebase/ml-vision', () => ( vision: jest.fn(() => ( ... )), ) );谢谢! @Heraldo,您也可以将其放在项目的*** __mocks__/@react-native-firebase/ml-vision/index.js 目录中。然后只需 module.exports = vision:()=>以上是关于React Native Expo - Jest - React Native Firebase - Invariant Violation:Native 模块不能为空的主要内容,如果未能解决你的问题,请参考以下文章
react-native-vector-icons/MaterialIcons jest-expo 快照测试错误与打字稿
在未弹出 Redux 的情况下测试 Jest React-Native Expo CRNA
React Native - JEST:没有找到 haste.hasteImplModulePath 选项中的 react-native/jest/hasteImpl.js
react-native, jest, ts-jest: ReferenceError: React is not defined