Istanbul code coverage for Mocha tests

三世轮回 提交于 2019-12-10 00:55:16

问题


I am trying to get Istanbul to work.

I keep getting this message at the end of running istanbul:

No coverage information was collected, exit without writing coverage information

I have tried everything I could find online as you can see:

      "scripts": {
        "start": "node ./bin/start.js",
        "test": "mocha test --no-timeouts",
        "debug_mocha": "node-debug --no-timeouts _mocha",
        "eslint": "eslint .",
        "jshint": "jshint --exclude ./node_modules .",
        "istanbul": "istanbul cover --include-all-sources --hook-run-in-context node_modules/.bin/_mocha -- -u exports -R spec test/**/*",
        "istanbul2":"istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/**/*",
        "istanbul1":"istanbul cover node_modules/.bin/_mocha -- test/**/*",
        "istanbul0":"istanbul cover _mocha test/**/*.js",
        "istanbul3":"istanbul cover _mocha -- -R spec --recursive test"
      }

my .istanbul.yml file is at the root of the project and looks like istanbul is picking it up successfully.

//.istanbul.yml
    instrumentation:
      compact: false
      save-baseline: true
    reporting:
      reports:
        - lcov
        - cobertura

What am I missing?


回答1:


Got it, finally.

https://github.com/gotwarlost/istanbul/issues/44#issuecomment-57708358 says:

Using _mocha directly does not work on windows. Please use the full path to the JS file instead

Then, after a little experimentation, victory:

istanbul cover C:/dev/my_project/node_modules/mocha/bin/_mocha --



回答2:


Try this, may works for you. Script tag in your package.json

"cover" : "NODE_ENV=test babel-node ./node_modules/babel-istanbul/lib/cli cover --dir ./coverage _mocha -- -R spec --ui bdd ./test/setup.js ./test --recursive"

setup.js will be your setup javascript

./test will be the folder where all your tests sits.

packages need to install:

  • babel-istanbul
  • babel-node
  • mocha

This works for me :) (npm run cover)


Note: if on windows: remove NODE_ENV=test, and may need to make the _mocha full path to your node module folder




回答3:


If you're setting up a new project and trying to get istanbul to work, ensure that you have a least one test file that references at least one of the project files for istanbul to reference for its coverage report.

"istanbul": "0.4.4",
   "mocha": "3.0.0"

In my case, I was setting up a new project and getting the first index.js and test/index.spec.js files prepped. When I tried to run:

"test": "istanbul cover --report html node_modules/mocha/bin/_mocha -- test/**/*.js --ui bdd -R spec"

I got an output from mocha of all unit tests passing (which there were no tests written yet so all 0 of them passed) but I then received this error after that:

No coverage information was collected, exit without writing coverage information

The issue turned out to be that I had not yet added a require for the index.js file in the test/index.spec.js file yet. Istanbul had no files to reference from the test files to check for coverage information.

If it's a pathing issue to _mocha referenced in the npm script, there will be an additional "SyntaxError:" message that follows the "No coverage information was collected" message.



来源:https://stackoverflow.com/questions/30770544/istanbul-code-coverage-for-mocha-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!