【问题标题】:React native jest: While resolving: image-jest@1.0.0 npm ERR! Found: react@17.0.1反应本机笑话:解决时:image-jest@1.0.0 npm ERR!找到:react@17.0.1
【发布时间】:2022-10-30 05:35:55
【问题描述】:

我正在尝试将@testing-library/react-native 安装到一个新创建的基于 expo 的 react 本机应用程序中。

为了添加测试用例,我按照https://docs.expo.dev/guides/testing-with-jest/ 中提到的步骤安装了jestjest-expo

之后,我尝试使用命令安装@testing-library/react-native

npm install --save-dev @testing-library/react-native

并开始出现以下错误

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: image-jest@1.0.0
npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR!   peer react@">=16.0.0" from @testing-library/react-native@9.0.0
npm ERR!   node_modules/@testing-library/react-native
npm ERR!     dev @testing-library/react-native@"*" from the root project
npm ERR!   1 more (react-native)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"17.0.2" from react-test-renderer@17.0.2
npm ERR! node_modules/react-test-renderer
npm ERR!   peer react-test-renderer@">=16.0.0" from @testing-library/react-native@9.0.0
npm ERR!   node_modules/@testing-library/react-native
npm ERR!     dev @testing-library/react-native@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

我的package.json 如下所示

{
  "name": "image-jest",
  "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",
    "eject": "expo eject",
    "test": "jest"
  },
  "dependencies": {
    "expo": "~44.0.0",
    "expo-status-bar": "~1.2.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-web": "0.17.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/jest": "^27.4.1",
    "@types/react": "~17.0.21",
    "@types/react-native": "~0.64.12",
    "jest": "^26.6.3",
    "jest-expo": "^44.0.1",
    "typescript": "~4.3.5"
  },
  "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
}

我无法理解导致问题的原因。谁能建议可以做些什么来解决它?

【问题讨论】:

    标签: react-native testing react-native-testing-library


    【解决方案1】:

    错误是因为反应测试渲染器@17.0.2需要反应@17.0.2, 而你有一个旧版本的反应。

    尝试安装旧版本的反应测试渲染器第一的:

    npm i react-test-renderer@17.0.1
    

    然后安装@testing-library/react-native

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2020-04-03
      • 2021-01-18
      相关资源
      最近更新 更多