运行带有React的Jest测试套件时,无法从“ react@16.4.1.js”中找到模块“ npm:react@16.4.1/index.js”

时间:2018-08-09 12:46:30

标签: reactjs jestjs babel enzyme

我正在尝试将项目从使用karmaphantomJs迁移到jest,但是我无法正确获取配置文件。我不断收到以下错误:

 FAIL  test/unit/components/SomethingWentWrong/SomethingWentWrong.spec.js
  ● Test suite failed to run

    Cannot find module 'npm:react@16.4.1/index.js' from 'react@16.4.1.js'

    > 1 | module.exports = require("npm:react@16.4.1/index.js");
        |                  ^

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (jspm_packages/npm/react@16.4.1.js:1:18)

这是我的babeljest配置的摘要。

... rest of code

  "devDependencies": {
    "@babel/core": "^7.0.0-beta.56",
    "babel-jest": "^23.4.2",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "jest": "^23.4.2",
    "react-test-renderer": "^16.4.2"
  },
  "jest": {
    "verbose": true,
    "testURL": "http://localhost/",
    "transform": {
      "^.+\\.(jsx|js)?$": "<rootDir>/node_modules/babel-jest"
    },
    "collectCoverageFrom": [ "!**/node_modules/**" ]
  },
  "babel": {
    "presets": [
      [
        "env",
        {
          "loose": true,
          "debug": false,
          "targets": {
            "browsers": [
              "last 3 versions"
            ]
          }
        }
      ],
      "react"
    ]
  }
... rest of code

如果有人可以帮助我和/或为我指明正确的方向,我不明白这是什么问题?非常感谢

1 个答案:

答案 0 :(得分:0)

我实际上发现了问题所在,因为该项目正在处理jspmnpm的依赖关系,由于某些原因,即使执行了npm install my_packagejspm install npm:my_package。快速浏览node_folder,确认即使软件包出现在package.json

上,它们也没有安装

npm cache verified和新的jspm install npm: my_package解决了该问题