Expo(React 18)项目安装React Native Testing Library报错求助
解决Expo + React 18下安装React Native Testing Library报错的方案
1. 安装适配React 18的最新版本
React Native Testing Library的最新稳定版已经完全支持React 18,直接执行安装命令:
npm install --save-dev @testing-library/react-native @testing-library/jest-native # 或使用yarn yarn add --dev @testing-library/react-native @testing-library/jest-native
2. 清理依赖缓存并重新安装
若仍存在报错,先清理缓存并重置依赖:
# 清理npm缓存 npm cache clean --force # 删除依赖目录和锁文件 rm -rf node_modules package-lock.json # 重新安装所有依赖 npm install
3. 配置Jest适配Expo环境
Expo自带Jest预设,确保你的jest.config.js(或package.json中的jest字段)配置正确:
module.exports = { preset: 'jest-expo', setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'], };
4. 统一React相关依赖版本
确保项目中react、react-dom、react-test-renderer版本一致且为18.x,执行以下命令检查版本:
npm list react react-dom react-test-renderer
若版本不一致,手动更新到同版本:
npm install react@18.x react-dom@18.x react-test-renderer@18.x --save
5. 临时绕过peer依赖冲突
如果报错是peer依赖不兼容导致的,可临时使用--legacy-peer-deps参数完成安装(优先建议升级依赖,此为临时方案):
npm install --save-dev @testing-library/react-native --legacy-peer-deps
内容的提问来源于stack exchange,提问作者Rafael
相关产品推荐
相关产品推荐

