ES6+mocha+istanbul,针对ES6语法的带覆盖率检查的mocha测试

安装 mocha, chai,mochawesome,istanbul

npm install mocha chai mochawesome [email protected] --save-dev

注意1:

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:

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

注意3:

为了使 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 都可以
ES6+mocha+istanbul,针对ES6语法的带覆盖率检查的mocha测试_第1张图片

参考:http://edu.51cto.com/topic/10...

你可能感兴趣的:(mocha,es6,测试,前端)