ES6+mocha+istanbul,針對ES6語法的帶覆蓋率檢查的mocha測試

安裝 mocha, chai,mochawesome,istanbul

npm install mocha chai mochawesome istanbul@1.0.0-alpha.2 --save-dev

注意1:html

istanbul 版本爲 "^1.0.0-alpha.2"

爲 istanbul 支持ES6語法安裝依賴包:

npm i babel-cli babel-register babel-plugin-istanbul babel-preset-env cross-env mocha chai nyc --save-dev

注意2:node

爲了使 istanbul 支持ES6語法,在package.json加入:
"nyc": {
    "require": [
      "babel-register"
    ],
    "reporter": [
      "lcov",
      "text"
    ],
    "sourceMap": false,
    "instrument": false
  }

注意3:npm

爲了使 istanbul 支持ES6語法,在.babelrc加入:

.babelrc

{
  "presets": [
    "env"
  ],
  "env": {
    "test": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

package.json

{
  "name": "100",
  "version": "1.0.0",
  "description": "小貓快跳",
  "main": "game.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test1": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha test/*.js -- --require babel-register",
    "test":"cross-env NODE_ENV=test nyc mocha"
  },
  "author": "wangshuxian6",
  "license": "MIT",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-istanbul": "^4.1.5",
    "babel-preset-env": "^1.6.1",
    "babel-register": "^6.26.0",
    "chai": "^4.1.2",
    "cross-env": "^5.1.3",
    "grunt": "^1.0.1",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-eslint": "^20.1.0",
    "istanbul": "^1.0.0-alpha.2",
    "mocha": "^5.0.1",
    "mochawesome": "^3.0.2",
    "nyc": "^11.4.1"
  },
  "nyc": {
    "require": [
      "babel-register"
    ],
    "reporter": [
      "lcov",
      "text"
    ],
    "sourceMap": false,
    "instrument": false
  }
}

mocha.opts

--require babel-core/register
--recursive
--reporter mochawesome
--bail

最終:

運行nom run test 或 nom run test2 均可以
圖片描述

參考:http://edu.51cto.com/topic/10...json

相關文章
相關標籤/搜索