如何在使用vue-cli初始化项目后添加Karma + Mocha测试

时间:2017-08-31 12:14:12

标签: webpack vuejs2 karma-mocha karma-webpack vue-cli

您好我使用Vue-cli webpack启动了一个项目我犯的错误是我没有启用测试

如何将karma + mocha添加到我的webpack vuejs2开发中是否可以重新初始化并包含测试?

1 个答案:

答案 0 :(得分:5)

你的package.json中的

添加以下内容:

"karma": "^1.7.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4",
"mocha": "^3.0.2",
"chai": "~4.1.1",

在karma.conf.js中:

const wpConf = require("./webpack.config")
wpConf.devtool = "inline-source-map"

module.exports = config => {
  config.set({
    browsers:      ["PhantomJS"],
    files:         ["./socialhome/**/app/tests/**/*\.tests\.js"],
    frameworks:    ["mocha"],
    plugins:       [
        "karma-phantomjs-launcher",
        "karma-mocha",
        "karma-sourcemap-loader",
        "karma-webpack"
    ],
    preprocessors: {"./socialhome/**/app/tests/**/*\.tests\.js": ["webpack", "sourcemap"]},
    reporters:     ["dots"],
    singleRun:     true,
    webpack:       wpConf
})
}

再次在你的package.json中:

"scripts": {
   "dev": "./node_modules/webpack/bin/webpack.js --watch",
   "test": "./node_modules/karma/bin/karma start"
}

为了使用最新的ECMAScript语法,我在启动测试之前通过webpack传递了Karma。

我希望这有帮助。