首页 > 解决方案 > 尝试在反应本机应用程序上运行 jest 时测试失败。找不到“setupDevtools”错误

问题描述

我正在尝试运行我的测试,但由于某种原因,我在执行npm run test.

  ● Test suite failed to run

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

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:9:6)

这是我的package.json

{
    "private": true,
    "name": "app",
    "version": "0.1.0",
    "scripts": {
    "start": "react-native start",
    "test": "jest",
    "test:watch": "jest -w",
  },
  "jest": {
    "preset": "react-native"
  },
  "dependencies": {
    "base-64": "^0.1.0",
    "date-fns": "^2.0.0-alpha.8",
    "hoist-non-react-statics": "^2.5.5",
    "react": "16.3.1",
    "react-native": "^0.55.4",
  },
  "devDependencies": {
    "babel-eslint": "^8.2.3",
    "babel-jest": "^24.7.1",
    "babel-plugin-transform-export-extensions": "^6.22.0",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-prettier": "^2.6.0",
    "eslint-plugin-react": "^7.8.2",
    "flow-bin": "^0.97.0",
    "flow-typed": "^2.5.1",
    "jest": "^24.7.1",
    "jest-react-native": "^18.0.0",
    "prettier": "^1.12.1",
    "react-devtools": "^3.2.2",
    "react-native-testing-library": "^1.7.0",
    "react-test-renderer": "16.3.1"
  }
}

标签: react-nativejestjs

解决方案


问题在于我的依赖项的版本不匹配。由于我正在使用

"react": "16.3.1",
"react-native": "^0.55.4"

我不得不从降级"jest": "^24.7.1""jest": "23.6.0". 之后遇到了一些问题babel。安装"@babel/core": "^7.4.5""@babel/runtime": "^7.4.5". 并完成整个安装"babel-jest": "^24.8.0", "@testing-library/jest-native": "^3.0.1" and "native-testing-library": "^3.1.1",

最后package.jsonreact-native@0.55.4

..omitted_code

"@babel/core": "^7.4.5",
"babel-jest": "^24.8.0", 
"@babel/runtime": "^7.4.5",
"jest": "23.6.0",
"@testing-library/jest-native": "^3.0.1",
"native-testing-library": "^3.1.1",

..omitted_code

推荐阅读