簡體   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