You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Expo与AWS Amplify时Jest无法运行测试的问题

React Native + AWS Amplify 测试依赖与NativeModule问题

我在为React Native项目的组件编写测试时,通过import { API } from "aws-amplify"模拟发起API调用的函数,先出现如下警告:

[WARN] 18:32.540 ClientDevice_Browser - No navigator object available to get browser client info

随后触发错误:

Cannot find module '@react-native-async-storage/async-storage' from 'node_modules/@aws-amplify/datastore/lib/storage/adapter/InMemoryStore.native.js'
...
1 | import { API } from "aws-amplify";
  | ^

我尝试安装@react-native-async-storage/async-storage后,又出现新的错误:

[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

另外,我还试过清除缓存、删除yarn.lock文件和node_modules文件夹后重新安装所有依赖,但问题依然没有解决。

项目package.json内容

{
  "main": "node_modules/expo/AppEntry.js",
  "engines": {
    "npm": "~8.19.3",
    "node": ">=16.0.0 <17.0.0"
  },
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest --forceExit",
    "prepare": "husky install"
  },
  "dependencies": {
    "@expo-google-fonts/inter": "^0.2.2",
    "@expo-google-fonts/work-sans": "^0.2.2",
    "@expo/vector-icons": "^13.0.0",
    "@react-native-community/datetimepicker": "6.2.0",
    "@react-native-community/netinfo": "9.3.0",
    "@react-native-picker/picker": "2.4.2",
    "@react-navigation/bottom-tabs": "^6.0.5",
    "@react-navigation/native": "^6.0.2",
    "@react-navigation/native-stack": "^6.1.0",
    "@unimodules/core": "~7.2.0",
    "@unimodules/react-native-adapter": "~6.5.0",
    "aws-amplify": "^4.3.12",
    "aws-amplify-react-native": "^6.0.2",
    "axios": "^0.22.0",
    "eslint-plugin-unused-imports": "^2.0.0",
    "expo": "^46.0.0",
    "expo-asset": "~8.6.3",
    "expo-constants": "~13.2.4",
    "expo-font": "~10.2.0",
    "expo-linear-gradient": "~11.4.0",
    "expo-linking": "~3.2.4",
    "expo-modules-core": "~0.11.10",
    "expo-splash-screen": "~0.16.2",
    "expo-status-bar": "~1.4.0",
    "expo-web-browser": "~11.0.0",
    "firebase": "8.2.3",
    "number-abbreviate": "^2.0.0",
    "postcode-validator": "^3.7.0",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-hook-form": "^7.17.1",
    "react-icons": "^4.4.0",
    "react-native": "0.68.2",
    "react-native-animated-skeleton": "^0.1.0",
    "react-native-dotenv": "^3.3.1",
    "react-native-elements": "^3.4.2",
    "react-native-gesture-handler": "~2.5.0",
    "react-native-keyboard-aware-scroll-view": "^0.9.4",
    "react-native-modal-datetime-picker": "^13.1.2",
    "react-native-progress": "^5.0.0",
    "react-native-reanimated": "~2.9.1",
    "react-native-safe-area-context": "4.3.1",
    "react-native-screens": "~3.15.0",
    "react-native-snap-carousel": "^3.9.1",
    "react-native-svg": "12.3.0",
    "react-native-vector-icons": "^9.0.0",
    "react-native-web": "~0.18.7",
    "zipcodes": "^8.0.0"
  },
  "devDependencies": {
    "@testing-library/react-native": "^11.5.2",
    "@types/jest": "^29.4.0",
    "@types/react": "18.0.0",
    "@types/react-native": "~0.67.6",
    "@types/react-native-datepicker": "^1.7.1",
    "@types/react-native-snap-carousel": "^3.8.4",
    "@typescript-eslint/eslint-plugin": "^5.29.0",
    "@typescript-eslint/parser": "^5.29.0",
    "eslint": "^8.18.0",
    "eslint-plugin-react": "^7.30.0",
    "husky": "^8.0.1",
    "jest": "^27.0.0",
    "jest-expo": "^47.0.1",
    "react-native-svg-transformer": "^0.14.3",
    "typescript": "~4.3.5"
  },
  "resolutions": {
    "@types/react": "18.0.0",
    "@types/react-dom": "18.0.0",
    "@expo/vector-icons": "^13.0.0"
  },
  "private": true,
  "jest": {
    "preset": "jest-expo"
  }
}

内容的提问来源于stack exchange,提问作者spap975

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 04:55:21