使用Jest+Expo+Firebase做集成测试时getFirestore函数报错
问题:Jest集成测试中Firebase v9
getFirestore 函数无法识别 在Expo应用的登录页面执行Jest集成测试时,出现错误:TypeError: (0 , _firestore.getFirestore) is not a function。应用本身运行正常,getFirestore函数能被正常识别,但在Jest测试环境下却无法识别。怀疑是因为Firebase配置使用了Expo要求的Web Version 9导致的,尝试过以下几种导入方式均无效:
import { firestore } from "firebase/firestore"import "firebase/firestore"export const db = app.firestore()
相关代码
Firebase配置文件
import { initializeApp } from 'firebase/app'; import { getFirestore } from "firebase/firestore"; // Firebase API const firebaseConfig = { }; const app = initializeApp(firebaseConfig); export const db = getFirestore(app);
package.json
{ "name": "test", "version": "1.0.0", "main": "node_modules/expo/AppEntry.js", "scripts": { "start": "expo start", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web" }, "dependencies": { "@emailjs/browser": "^3.10.0", "@expo/dev-server": "0.1.120", "@expo/webpack-config": "^0.17.0", "@react-native-async-storage/async-storage": "~1.17.3", "@react-native-community/clipboard": "^1.5.1", "@react-native-community/datetimepicker": "6.5.2", "@react-native-community/masked-view": "^0.1.11", "@react-native-picker/picker": "2.4.8", "@react-navigation/bottom-tabs": "^6.4.0", "@react-navigation/native": "^6.0.13", "@react-navigation/stack": "^6.3.3", "@twotalltotems/react-native-otp-input": "1.3.7", "base-64": "^1.0.0", "expo": "^47.0.8", "expo-checkbox": "~2.2.0", "expo-document-picker": "~11.0.1", "expo-image-picker": "~14.0.2", "expo-location": "~15.0.1", "expo-status-bar": "~1.4.0", "expo-web-browser": "~12.0.0", "firebase": "^9.16.0", "jest": "^26.6.3", "jest-expo": "^47.0.1", "leo-profanity": "^1.7.0", "lottie-react-native": "^5.1.4", "moment": "^2.29.4", "parse": "^4.0.0", "react": "18.1.0", "react-dom": "18.1.0", "react-hook-form": "^7.42.0", "react-native": "0.70.5", "react-native-draggable-flatlist": "^4.0.0", "react-native-dropdown-select-list": "^2.0.4", "react-native-gesture-handler": "~2.8.0", "react-native-google-places-autocomplete": "^2.5.1", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-keychain": "^8.1.1", "react-native-maps": "1.3.2", "react-native-maps-directions": "^1.9.0", "react-native-modal-datetime-picker": "^14.0.1", "react-native-otp-input": "^1.0.12", "react-native-paper": "^4.12.5", "react-native-picker-select": "^8.0.4", "react-native-reanimated": "~2.12.0", "react-native-reanimated-carousel": "^3.1.5", "react-native-safe-area-context": "4.4.1", "react-native-screens": "~3.18.0", "react-native-uuid": "^2.0.1", "react-native-web": "~0.18.7", "react-native-webview": "11.23.1", "rn-credit-card": "^1.0.4", "uniqid": "^5.4.0" }, "devDependencies": { "@babel/core": "^7.12.9", "@testing-library/react-native": "^11.5.1" }, "jest": { "preset": "jest-expo", "transformIgnorePatterns": [ "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)" ] }, "private": true }
解决方案
1. 调整Jest的transformIgnorePatterns
当前的transformIgnorePatterns未包含Firebase相关模块,导致Jest没有转译Firebase v9的ES模块语法。修改jest配置,添加firebase和@firebase到白名单:
"jest": { "preset": "jest-expo", "transformIgnorePatterns": [ "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|firebase|@firebase)" ] }
2. 模拟Firestore模块
直接在测试中模拟Firestore模块,避免调用真实Firebase API。创建__mocks__/firebase/firestore.js文件:
export const getFirestore = jest.fn(() => ({ // 按需模拟Firestore方法,比如collection、doc等 collection: jest.fn(() => ({ add: jest.fn(), get: jest.fn() })) }));
然后在测试文件顶部添加:
jest.mock('firebase/firestore');
3. 降级Firebase版本(不推荐)
如果以上方法无效,可尝试降级到Firebase v8兼容版本,但可能与Expo要求冲突,需谨慎操作。
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

