繁体   English   中英

Windows 7中Mocha的代码覆盖率

[英]Code coverage for Mocha in Windows 7

我正在尝试使用我在全球安装的istanbulmocha进行代码覆盖:

按照这里的建议这样做

  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

此命令也会抛出与上面相同的错误:

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

我被一个github问题告诉我,我必须从node_modules添加一条到mocha的路径,所以我这样做了:

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

规范

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

我使用Windows 7作为我的操作系统

我的测试看起来像这样:

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]);
    })
  })
})  

这对我有用:

我在本地安装了mocha和istanbul(在npm安装上没有-g)。 我之前在跑步

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

这就产生了你上面看到的错误。 我已将其更改为查看mocha的显式本地文件夹

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

现在一切都很好。

请注意我在Windows 7上的gitbash下运行我的设置

尝试这个,它应该工作。

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

根据github中的这个评论 ,在windows中批处理命令没有在javascript中解析。 因此引起了这个问题。

根据建议,您可以直接将mocha的javascript函数传递给istanbul。 这是你如何做到的

  • 输入npm ls -g --depth=0
  • 复制输出的第一行。 它应该是C:\\Users\\<YourUserName>\\AppData\\Roaming\\npm
  • 键入istanbul cover C:\\Users\\<YourUserName>\\AppData\\Roaming\\npm\\node_modules\\mocha\\bin\\_mocha test\\folder

所以这里没有传递_mocha(windows命令),而是传递了来自全局mocha安装的javascript。 这正是前一个答案中为mocha本地安装所发生的事情。

试试这个命令:

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

username =您的用户名

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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