安装 mocha, chai,mochawesome,istanbul

npm install mocha chai mochawesome istanbul@1.0.0-alpha.2 --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 都可以
图片描述

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


Airmusic
228 声望14 粉丝

了解 vue 源码,k8s。熟悉常用前端框架、库、工具:Vue, Angular,React, Webpack,Gulp 等 了解 node, 熟悉 KOA 熟练使用 Git,熟练 Gitflow 等常见开发实践 注重设计细节和用户体验,有较强的审美和自主设计能力...