升级React Native到0.63.4后运行Jest报错:无法解构instrument属性
React Native 0.63.4 Jest 运行报错问题解决方案
根因分析
你当前的报错本质是Jest版本和React Native版本不兼容:
React Native 0.63.4官方适配的Jest最高版本为26.x,你使用的Jest 27.x版本做了大量API破坏性变更,旧版react-native预设依赖的fbjs-scripts无法适配Jest 27的新API,无法读取instrument属性从而抛出错误。
另外你将jest、babel-jest等测试相关依赖放到了生产依赖dependencies中,也属于不规范的配置,建议统一移到开发依赖devDependencies。
解决步骤
- 卸载现有高版本Jest相关依赖
执行命令:npm uninstall jest jest-cli babel-jest - 安装React Native 0.63.x匹配的Jest版本
执行命令:npm install --save-dev jest@26.6.3 babel-jest@26.6.3 - 清理冗余缓存和依赖文件
执行命令:rm -rf node_modules package-lock.json yarn.lock npm install npx jest --clearCache - (可选)检查Babel配置
确认项目根目录下的babel.config.js配置符合RN 0.63的要求:module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: ['@babel/plugin-proposal-class-properties'] };
配置完成后重新执行npm test,即可正常运行测试用例。
内容的提问来源于stack exchange,提问作者Aaron
相关产品推荐
相关产品推荐

