酶和摩卡给我伊斯坦布尔覆盖测试的错误,为什么?

Enzyme and Mocha gives me error for a Istanbul coverage test, why?

我有一个 ReactJs 项目,我正在使用 Enzyme 和 Mocha 进行单元测试。 我设置我的 package.json 来执行这个命令:

./node_modules/.bin/mocha 
  --compilers js:babel-register 
  --require 'app/tests/support/setup.mocha.js' 
  --recursive 'app/tests/**/*.test.js'

我想要一份报道报告,所以我决定去伊斯坦布尔。 我安装了它,据我了解这是基本命令

istanbul cover _mocha

但是我在执行我的覆盖脚本时遇到了这样的错误:

./node_modules/.bin/istanbul cover   
  ./node_modules/.bin/_mocha
  --compilers js:babel-register 
  --require 'app/tests/support/setup.mocha.js' 
  --recursive 'app/tests/**/*.test.js'

我有以下错误:

/node_modules/mocha/lib/utils.js:634 throw new Error("cannot resolve path (or pattern) '" + path + "'");

Error: cannot resolve path (or pattern) 'js:babel-register' at Object.lookupFiles (/node_modules/mocha/lib/utils.js:634:15) at /node_modules/mocha/bin/_mocha:327:30 at Array.forEach (native) at Object. (/node_modules/mocha/bin/_mocha:326:6) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Object.Module._extensions.(anonymous function) [as .js] (/node_modules/istanbul/lib/hook.js:109:37) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at runFn (/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16) at /node_modules/istanbul/lib/command/common/run-with-cover.js:251:17 at /node_modules/istanbul/lib/util/file-matcher.js:68:16 at /node_modules/istanbul/node_modules/async/lib/async.js:52:16 at /node_modules/istanbul/node_modules/async/lib/async.js:361:13 at /node_modules/istanbul/node_modules/async/lib/async.js:52:16 at done (/node_modules/istanbul/node_modules/async/lib/async.js:246:17) at /node_modules/istanbul/node_modules/async/lib/async.js:44:16 at /node_modules/istanbul/node_modules/async/lib/async.js:358:17 at LOOP (fs.js:1530:14) at nextTickCallbackWith0Args (node.js:420:9) at process._tickCallback (node.js:349:13)

这是文件结构,我的脚本是从 /web/ 文件夹执行的

这是 package.json

{
  "name": "XXX",
  "version": "XXXX",
  "description": "XXXX",
  "scripts": {
    "start": "cross-env NODE_PATH=./app NODE_ENV=development concurrently --kill-others \"nodemon app/server\" \"node webpack.server.js\" \"gulp css:watch\"",
    "ci-server": "cross-env NODE_PATH=./app PORT=3001 CONFIG_FILE=features/ci.web.config.json node index",
    "prod-server": "cross-env NODE_PATH=./app NODE_ENV=production node index",
    "test": "cross-env NODE_ENV=test cross-env NODE_PATH=./app mocha --compilers js:babel-register --require 'app/tests/support/setup.mocha.js' --recursive 'app/tests/**/*.test.js' -w",
    "test:ci": "cross-env NODE_ENV=test cross-env NODE_PATH=./app mocha --compilers js:babel-register --require 'app/tests/support/setup.mocha.js' --recursive 'app/tests/**/*.test.js'",
    "webpack": "webpack",
    "build": "rm -rf dist/*.css && rm -rf dist/*.js* && npm run webpack && gulp css:build",
    "functional-test": "node --stack-size=10000 --harmony ./node_modules/.bin/cucumber.js -b",
    "postinstall": "npm run build",
    "eslint": "node_modules/.bin/eslint --config=.eslintrc app/* features/support/* features/step_definitions/*",
    "csslint": "stylelint --config=.stylelintrc.json 'app/shared/components/**/**.scss'"
  },
  "dependencies": {
    "assets-webpack-plugin": "^3.2.0",
    "babel-polyfill": "^6.3.14",
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-es2015-loose": "^7.0.0",
    "babel-register": "^6.14.0",
    "bluebird": "^2.10.0",
    "body-parser": "^1.15.2",
    "bourbon": "^4.2.7",
    "classlist-polyfill": "^1.0.3",
    "classnames": "^2.2.5",
    "compression": "^1.6.0",
    "cookie-parser": "^1.4.3",
    "ejs": "^2.3.4",
    "express": "^4.13.3",
    "file-system": "^2.2.1",
    "glob": "^7.0.0",
    "history": "^1.17.0",
    "http-proxy": "^1.14.0",
    "humps": "^0.6.0",
    "immutable": "^3.8.1",
    "isomorphic-fetch": "^2.1.1",
    "json-loader": "^0.5.4",
    "lodash": "^4.15.0",
    "moment": "^2.14.1",
    "normalize-scss": "^5.0.3",
    "normalizr": "^1.0.0",
    "react": "^15.0.2",
    "react-dom": "^15.0.2",
    "react-player": "^0.9.0",
    "react-redux": "^2.1.2",
    "react-router": "^2.4.1",
    "react-router-redux": "^4.0.5",
    "react-twitter-widgets": "^0.2.4",
    "redux": "^3.0.0",
    "redux-logger": "^1.0.9",
    "redux-thunk": "^0.1.0",
    "request": "^2.74.0",
    "rewire": "^2.3.4",
    "stylelint": "^7.0.3",
    "superagent": "^1.4.0",
    "velocity-animate": "^1.2.3"
  },
  "devDependencies": {
    "babel-cli": "^6.11.4",
    "babel-core": "^6.3.15",
    "babel-eslint": "^6.0.4",
    "babel-loader": "^6.2.0",
    "babel-plugin-react-transform": "^2.0.0",
    "babel-plugin-rewire": "^1.0.0-beta-5",
    "babel-preset-react": "^6.11.1",
    "babel-preset-react-hmre": "^1.0.1",
    "babel-preset-stage-0": "^6.5.0",
    "chai": "^3.3.0",
    "chai-as-promised": "^5.1.0",
    "concurrently": "^2.0.0",
    "cross-env": "^1.0.7",
    "cucumber": "^1.2.0",
    "cucumber-assert": "^1.0.4",
    "enzyme": "^2.3.0",
    "eslint": "^2.10.2",
    "eslint-config-mocha": "0.0.0",
    "eslint-config-standard": "^5.3.1",
    "eslint-plugin-import": "^1.10.2",
    "eslint-plugin-mocha": "^4.0.0",
    "eslint-plugin-mocha-only": "0.0.3",
    "eslint-plugin-promise": "^1.3.2",
    "eslint-plugin-react": "^5.1.1",
    "eslint-plugin-standard": "^1.3.2",
    "express-useragent": "^0.2.4",
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-load-plugins": "^1.2.0",
    "gulp-rev": "^6.0.1",
    "gulp-sass": "^2.1.1",
    "gulp-sass-glob": "^1.0.6",
    "gulp-sourcemaps": "^1.6.0",
    "istanbul": "^0.4.5",
    "jsdom": "^7.2.2",
    "mocha": "^2.4.5",
    "mocha-istanbul": "^0.3.0",
    "nock": "^8.0.0",
    "node-sass": "^3.4.2",
    "nodemon": "^1.6.0",
    "react-addons-test-utils": "^15.0.2",
    "react-helmet": "^3.1.0",
    "react-transform-catch-errors": "^1.0.0",
    "react-transform-hmr": "^1.0.2",
    "redux-devtools": "^3.3.1",
    "redux-mock-store": "^1.2.0",
    "rewire-webpack": "^1.0.0",
    "rimraf": "^2.5.2",
    "sinon": "^1.17.1",
    "sinon-chai": "^2.8.0",
    "webpack": "^1.9.11",
    "webpack-dev-server": "^1.9.0"
  }
}

有人可以帮帮我吗?提前谢谢你

试试这个方法:

./node_modules/.bin/istanbul cover 
./node_modules/.bin/_mocha 
  --
  --compilers js:babel-register 
  --require 'app/tests/support/setup.mocha.js' 
  --recursive 'app/tests/**/*.test.js'

请注意,命令名称和参数之间的 you should pass -- 是为了告诉 istanbul 将剩余的参数传递给 mocha