简体   繁体   中英

Code coverage for Mocha in Windows 7

I am trying to do code coverage for mocha using istanbul which I have installed globally:

I do this as suggested here

  E:\Do\learn_mocha>istanbul cover _mocha -- -R spec

C:\Users\Vamsi\AppData\Roaming\npm\_mocha.CMD:1
(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0
                                                              ^
No coverage information was collected, exit without writing coverage information

SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:439:25)
    at Module._extensions..js (module.js:474:10)
    at Object.Module._extensions..js (C:\Users\Vamsi\AppData\Roaming\npm\node_mo
dules\istanbul\lib\hook.js:102:13)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at runFn (C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\comma
nd\common\run-with-cover.js:114:16)
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\command\comm
on\run-with-cover.js:232:17
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-ma
tcher.js:56:16
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-ma
tcher.js:35:9

This command also throws the same error as above:

    E:\Do\learn_mocha>istanbul cover --hook-run-in-context _mocha -- -R spec

I was told by a github issue that I had to add a path to mocha from node_modules ,so I did this:

    E:\Do\learn_mocha>istanbul cover C:\Users\Vamsi\AppData\Roaming\npm\mocha -- -R

spec

C:\Users\Vamsi\AppData\Roaming\npm\mocha:2
basedir=`dirname "$0"`
        ^
No coverage information was collected, exit without writing coverage information

SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:439:25)
    at Module._extensions..js (module.js:474:10)
    at Object.Module._extensions..js (C:\Users\Vamsi\AppData\Roaming\npm\node_mo
dules\istanbul\lib\hook.js:102:13)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at runFn (C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\comma
nd\common\run-with-cover.js:114:16)
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\command\comm
on\run-with-cover.js:232:17
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-ma
tcher.js:56:16
    at C:\Users\Vamsi\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-ma
tcher.js:35:9

I am using Windows 7 as my OS

My test looks like this:

var assert = require("assert"); // core module
var C = require('../cash.js');  // our module

describe('Cash Register', function(){
  describe('Module C', function(){

    it('should have a getChange Method', function(){
      assert.equal(typeof C, 'object');
      assert.equal(typeof C.getChange, 'function');
    })

    it('getChange(210,300) should equal [50,20,20]',function(){
        assert.deepEqual(C.getChange(210,300),[50,20,20]);
    })

    it('getChange(486,1000) should equal [500,10,2,2]',function(){
        assert.deepEqual(C.getChange(486,1000),[500,10,2,2]);
    })

    it('getChange(1487,10000) should equal [5000,2000,1000,500,10,2,1]',function(){
        assert.deepEqual(C.getChange(1487,10000),[5000,2000,1000,500,10,2,1]);
    })
  })
})  

This worked for me:

I have both mocha and istanbul installed locally (no -g on the npm install). I was running

*./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha*

and this came up with the error you were seeing above. I have changed this to look in the explicit local folder for mocha

*./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha*

and now everything is working great.

Please note I'm running my setup under gitbash on windows 7

尝试这个,它应该工作。

istanbul cover %APPDATA%/npm/node_modules/mocha/bin/_mocha -- -R spec

As per this comment in github, in windows the batch commands are not being resolved inside javascript. Hence causing the issue.

As per the suggestion, you can directly pass the javascript function for mocha to istanbul. Here is how you can do it

  • Type npm ls -g --depth=0
  • Copy the first line of the output. it should be C:\\Users\\<YourUserName>\\AppData\\Roaming\\npm
  • Type istanbul cover C:\\Users\\<YourUserName>\\AppData\\Roaming\\npm\\node_modules\\mocha\\bin\\_mocha test\\folder

So here instead of passing the _mocha(windows command), the javascript from your global mocha installation is being passed. This is exactly what, happening for the local installation of mocha in the previous answer.

Try this command:

istanbul cover C:\\Users\\username\\AppData\\Roaming\\npm\\node_modules\\mocha\\bin\\_mocha
                          ^
                          Insert Your username here

Where username = Your username

尝试:伊斯坦布尔封面%NODE_HOME%\\ node_modules \\ mocha \\ bin_mocha - -R spec --recursive test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM