【问题标题】:Getting Cannot find module 'react-apollo/test-utils' error in jest teact native在开玩笑的本机反应中获取找不到模块'react-apollo/test-utils'错误
【发布时间】:2022-01-01 16:26:35
【问题描述】:

我正在使用 jest 进行单元测试。但是低于错误。我尝试了一些解决方案但仍然相同。请建议我哪里错了。

失败测试/login-test.js ● 测试套件无法运行

Cannot find module 'react-apollo/test-utils' from 'login-test.js'

  3 | import Login from '../app/components/Login';
  4 | import LoginContainer from '../app/components/container/LoginContainer';
> 5 | import { MockedProvider } from 'react-apollo/test-utils';
    | ^

// package.json

"devDependencies": {
    "@babel/core": "7.9.0",
    "@babel/runtime": "7.9.2",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-cli": "^6.26.0",
    "babel-jest": "^24.9.0",
    "eslint": "6.8.0",
    "jest": "^24.9.0",
    "jetifier": "^1.6.4",
    "metro-react-native-babel-preset": "^0.56.0",
    "react-test-renderer": "16.8.6"
  },
  "jest": {
    "preset": "react-native",
    "collectCoverage": true,
    "coverageReporters": [
      "json",
      "html"
    ],
    "setupFiles": [
      "./jestSetupFile.js",
      "./node_modules/react-native-gesture-handler/jestSetup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|tcomb-form-native|@react-native-community/async-storage|native-base/node_modules/react-native-vector-icons|react-navigation|@codler/react-native-keyboard-aware-scroll-view|react-native-device-info|rn-sliding-up-panel|@react-native-community/masked-view|@react-navigation/bottom-tabs/node_modules/react-native-iphone-x-helper|rn-tooltip|@react-native-community/datetimepicker|node_modules/react-native-tab-view|jsbarcode|@react-native-community/art|@react-native-firebase/analytics|@react-native-firebase|jail-monkey|@dudigital/react-native-zoomable-view/src/ReactNativeZoomableView|@haskkor/react-native-pincode|Platform)"
    ]
  },
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }

【问题讨论】:

  • 你的 package.json 中的 react-apollo 在哪里?以及您使用的是什么版本的 Apollo 客户端?
  • @Fraction "apollo-link-context": "1.0.20", "apollo-boost": "0.4.9", "react-apollo": "3.1.5",跨度>

标签: reactjs react-native jestjs graphql react-apollo


【解决方案1】:

您必须将导入更新为:

import { MockedProvider } from '@apollo/client/testing';

来自Apollo Client 3.0 migration doc:

react-apollov3 是一个伞包,重新导出以下内容 包:

  • @apollo/react-common
  • @apollo/react-hooks
  • @apollo/react-components
  • @apollo/react-hoc
  • @apollo/react-ssr
  • @apollo/react-testing

react-apollo 包已被弃用,功能 上述每个软件包提供的现在可以从 直接@apollo/client:

  • @apollo/react-hooks -> 现在可以直接从 @apollo/client 获得
  • @apollo/react-components -> 现在可从@apollo/client/react/components 获得
  • @apollo/react-hoc -> 现在可从 @apollo/client/react/hoc 获得
  • @apollo/react-ssr -> 现在可从@apollo/client/react/ssr 获得
  • @apollo/react-testing -> 现在可从 @apollo/client/testing 获得

【讨论】:

  • 感谢您的宝贵时间,但仍然出现同样的错误 ● 测试套件未能运行 找不到模块'@apollo/client/testing'
  • 你确定你已经安装了@apollo/client 吗?如果您正在使用纱线,请尝试 npm install @apollo/client 或 yarn add @apollo/client
  • 非常感谢它的工作
  • 我不确定您使用的是什么版本的 Apollo Client,但我假设它是 v3 (@apollo/client),因为您说您使用的是 react-apollo": "3.1.5",但以防万一重新使用v2(apollo-client,这是一个旧版本)你需要安装npm install react-apollo@2.5.8 --save并保留import { MockedProvider } from 'react-apollo/test-utils';
猜你喜欢
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 2021-05-30
  • 2019-12-09
  • 2021-10-15
  • 2020-12-21
相关资源
最近更新 更多