【问题标题】:Code coverage for Mocha in Windows 7Windows 7 中 Mocha 的代码覆盖率
【发布时间】:2014-11-23 01:00:10
【问题描述】:

我正在尝试使用已在全球范围内安装的 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]);
    })
  })
})  

【问题讨论】:

    标签: windows node.js mocha.js istanbul


    【解决方案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 下运行我的设置

    【讨论】:

    • 同样适用于 Jasmine:istanbul cover node_modules/jasmine/bin/jasmine
    • 有人知道是什么导致了这种行为吗?我也可以在我的电脑上复制
    • @Tim, istanbul cover 获取一个 JavaScript 文件。在 Linux 上,_mocha 只是一个 JS 文件,但在 Windows 上,它是一个可执行的批处理文件,Istanbul 无法处理。我猜茉莉花也是如此
    【解决方案2】:

    试试这个,应该可以的。

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

    【讨论】:

    • 对于部分Windows 10用户,默认npm全局目录安装配置在%AppData%/Roaming/
    【解决方案3】:

    根据 github 中的 comment,在 Windows 中,批处理命令没有在 javascript 中解析。因此导致了这个问题。

    根据建议,您可以直接将 mocha 的 javascript 函数传递到伊斯坦布尔。这是你可以做到的方法

    • 输入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 所发生的情况。

    【讨论】:

      【解决方案4】:

      试试这个命令:

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

      username = 您的用户名

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2013-05-14
          • 1970-01-01
          • 1970-01-01
          • 2018-05-21
          • 2015-03-22
          • 2018-07-20
          • 2015-07-03
          • 1970-01-01
          • 2012-06-30
          相关资源
          最近更新 更多