美文网首页
使用Mocha和Istanbul的一些问题

使用Mocha和Istanbul的一些问题

作者: bianer233 | 来源:发表于2017-03-01 21:58 被阅读1337次

    No coverage information was collected, exit without writing coverage information es6

    解决方法1:

    有问题的branch:https://github.com/JakeSidSmith/istanbul-no-coverage
    运行成功的branch:https://github.com/JakeSidSmith/istanbul-no-coverage/pull/1

       "scripts": {
          "start": "http-server . -c-1 -o",
          "build": "mkdir -p build/js/ && browserify -t babelify src/js/index.js -o build/js/index.js",
     -    "test": "istanbul cover node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --require test-helper.js --harmony --bail --recursive 'src/js/__tests__/**/*.test.js'"
     +    "test": "babel-node ./node_modules/istanbul/lib/cli cover node_modules/mocha/bin/_mocha -- --require test-helper.js --bail --recursive 'src/js/__tests__/**/*.test.js'"
        },
        "author": "",
        "license": "ISC",
     @@ -20,11 +20,12 @@
          "react-dom": "=0.14.2"
        },
        "devDependencies": {
     +    "babel-cli": "^6.2.0",
          "babel-core": "=6.2.1",
          "chai": "=3.4.1",
          "chai-immutable": "=1.5.3",
          "http-server": "=0.8.5",
     -    "istanbul": "=0.4.1",
     +    "istanbul": "^1.0.0-alpha",
          "jsdom": "=7.1.0",
          "mocha": "=2.3.4",
          "react-addons-test-utils": "=0.14.2",
    

    测验有效,但是我istanbul版本是最新的"istanbul":"^0.4.5",而且作者也说这种方法在其他版本里面可能会出现问题。

    解决方法2:

    使用babel-istanbul in lieu of istanbul

    "test:coverage": "babel-node ./node_modules/.bin/babel-istanbul cover ./node_modules/.bin/_mocha ./src/**/__tests__/*.js"

    stackoverflow原问题地址

    Transformation error; return original code

    创建一个.istanbul.yml文件,内容

    instrumentation:
        root: src
    

    或者在命令行添加 --root /src
    在使用了babel-istanbul之后,这个问题不需要.istanbul.yml文件和命令行设置也可以工作了。

    更多的讨论

    Window is not defined

    可以用jsdom-global来fix这个问题,介绍很清晰明了。

    jsdom-global will inject document, window and other DOM API into your Node.js environment. Useful for running, in Node.js, tests that are made for browsers.

    npm install --save-dev --save-exact jsdom jsdom-global

    然后把-r jsdom-global/register加到mocha的command line里面。

    最后我在package.json中的设置
    "test-cov": "babel-node ./node_modules/.bin/babel-istanbul cover _mocha -- --recursive './frontend/**/*.test.js' -r jsdom-global/register"

    运行结果:

    25 passing (57ms)  
    
    =============================== Coverage summary ===============================
    Statements   : 81.96% ( 559/682 )
    Branches     : 88.46% ( 150/169 ), 2 ignored
    Functions    : 81.43% ( 114/140 )
    Lines        : 80.67% ( 480/595 )
    ================================================================================
    

    相关文章

      网友评论

          本文标题:使用Mocha和Istanbul的一些问题

          本文链接:https://www.haomeiwen.com/subject/tdbcgttx.html