简体   繁体   English

找不到模块'setupDevtools'React-Native

[英]Cannot find module 'setupDevtools' React-Native

I am getting the following error: 我收到以下错误:

Cannot find module 'setupDevtools' from 'setup.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17

My JSON.package file looks like this: 我的JSON.package文件如下所示:

{
  "name": "MiBase",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "babel-preset-env": "^1.7.0",
    "i": "^0.3.6",
    "jest-react-native": "^18.0.0",
    "react": "16.3.1",
    "react-native": "0.55.3",
    "react-native-datepicker": "^1.7.2",
    "react-native-image-picker": "^0.26.7",
    "react-native-ui-xg": "0.0.6",
    "react-navigation": "^1.5.11"
  },
  "devDependencies": {
    "babel-jest": "^22.4.3",
    "babel-preset-react-native": "3.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "^16.3.1"
  },
  "jest": {
     "preset": "react-native",
     "moduleNameMapper": {
     "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__tests__/loginTest.js",
     "\\.(css|sass)$": "<rootDir>/__tests__/loginTest.js"
   },
   "transformIgnorePatterns": [
     "node_modules"
   ],
   "coveragePathIgnorePatterns": [
     "node_modules"
   ],
     "modulePathIgnorePatterns": [
     "node_modules"
   ]
 }

} }

My test file looks like this: 我的测试文件如下所示:

// __tests__/loginTest.js
import 'react-native';
import React from 'react';
import HomeSreen from '../App';

import renderer from 'react-test-renderer';

test('renders correctly', () => {
  const tree = renderer.create(<LoginScreen />).toJSON();
  expect(tree).toMatchSnapshot();
});

I am very new to React Native and even newer to testing with it. 我对React Native很新,甚至更新用它进行测试。 Everything I have learnt or have tried to solve this issue has come from these three places: 我学到或试图解决这个问题的一切都来自这三个地方:

https://facebook.github.io/jest/docs/en/tutorial-react-native.html Jest Cannot find module 'setupDevtools' from 'setup.js' Jest - Cannot find module 'setupDevtools' from 'setup.js' https://facebook.github.io/jest/docs/en/tutorial-react-native.html Jest无法从'setup.js'中 找到模块'setupDevtools'Jest - 无法 从'setup.js' 找到模块'setupDevtools'

I also have other functions in the class that I am not trying to test at this point. 我在课堂上也有其他功能,我现在不想测试。 I am only trying to test the GUI functionality and that everything displays correctly. 我只是试图测试GUI功能,一切都正确显示。 These functions are for processing http responses and JSON parsing. 这些函数用于处理http响应和JSON解析。

In my case I work with TypeScript and I had the same error and fix it with the following package.json configuration: 在我的情况下,我使用TypeScript,我有相同的错误,并使用以下package.json配置修复它:

"dependencies": {
    "react": "^16.3.0-alpha.1",
    "react-native": "0.54.3"
},
"devDependencies": {
    "@types/jest": "^22.2.2",
    "@types/react": "^16.0.41",
    "@types/react-native": "^0.52.19",
    "@types/react-test-renderer": "^16.0.1",
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-addons-test-utils": "^15.6.2",
    "react-native-mock": "^0.3.1",
    "react-native-typescript-transformer": "^1.2.3",
    "react-test-renderer": "^16.3.0-alpha.1",
    "ts-jest": "^22.4.2",
    "typescript": "^2.8.1"
},
"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js"
    ],
    "transform": {
        "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
        "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "testPathIgnorePatterns": [
        "\\.snap$",
        "<rootDir>/node_modules/",
        "<rootDir>/lib/"
    ],
    "cacheDirectory": ".jest/cache"
}

You can find this config on the this repo 你可以在这个repo上找到这个配置

I hope I've helped. 我希望我能帮到你。

I solved this same issue with below configuration: 我通过以下配置解决了同样的问题:

My jest / enzyme configuration: 我的开胃菜/酶配置:

// package.json
 "jest": {
    "preset": "react-native",
    "setupFiles": [
      "./config/enzyme.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 90,
        "functions": 90,
        "lines": 95,
        "statements": 96
      }
    },
    "moduleFileExtensions": [
      "js"
    ],
    "coverageDirectory": "coverage",
    "collectCoverageFrom": [
      "src/**/*.js"
    ]
  }

Jest setup file: Jest设置文件:

// config/enzyme.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

Example: @rimiti/react-native-toastify 示例: @ rimiti / react-native-toastify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块'react-native' - Cannot Find Module 'react-native' BABEL 找不到模块“节点:路径”错误 react-native - BABEL Cannot find module 'node:path' error react-native 错误:找不到模块“ reative-native” /未找到模块:错误:无法解析模块“ react-native-web” - Error: Cannot find module “react-native” / Module not found: Error: Cannot resolve module 'react-native-web' 错误:index.js: [BABEL]: 在 react-native 中找不到模块 'node:fs' - error: index.js: [BABEL]: Cannot find module 'node:fs' in react-native 在 expo react-native 项目中找不到模块“metro/src/lib/TerminalReporter” - Cannot find module 'metro/src/lib/TerminalReporter' in expo react-native project 找不到模块“react-native”的声明文件 - Could not find a declaration file for module 'react-native' 反应本地博览会找不到模块&#39;minizlib&#39; - react native expo Cannot find module 'minizlib' React Native 无法 npx init 项目,找不到模块错误 - React Native cannot npx init project, cannot find module error 运行测试时无法从“node_modules/react-native/jest/setup.js”中找到模块“@babel/runtime/helpers/interopRequireDefault” - Cannot find module '@babel/runtime/helpers/interopRequireDefault' from 'node_modules/react-native/jest/setup.js' when I run tests 无法解决模块“事件” React-Native - Unable to resolve module “events” React-Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM