windows - Windows 7 中 Mocha 的代码覆盖率

标签 windows node.js mocha.js istanbul

我正在尝试使用我在全局安装的 istanbulmocha 做代码覆盖:

我这样做 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

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

    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 下运行我的设置

关于windows - Windows 7 中 Mocha 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27084392/

相关文章:

mocha.js - 将 karma-coverage 中的 Istanbul 尔测试覆盖率与其他来源的覆盖率合并

c++ - 在 Windows 上使用 native C++ 在具有多个网络适配器的机器上进行 UDP 广播

c++ - 如何在 Windows 中用 c/c++ 从流中捕获视频?

javascript - 如何将变量从路由传递到 server.js

node.js - 使用 Heroku 构建可扩展分析后端的最佳方法是什么?

perl - 我如何将 Perl 的 'prove' 与其他语言的 TAP 生产者一起使用?

javascript - Mocha + react : navigator is not defined

python - 确定可执行文件(或库)是 32 位还是 64 位(在 Windows 上)

windows - 服务 OnExecute 失败,生成的线程未执行

node.js - nodejs 应用程序 - mongodb 连接失败,错误 "ECONNREFUSED"