VUE CLI 3-使用Mocha + Webpack测试单一文件组件

时间:2019-01-31 06:12:41

标签: testing vue.js webpack mocha vue-cli-3

我正在尝试遵循Vue Test Utils此处提到的文档。

下面是我当前设置的内容。

项目设置:带有节点v8.11.2的Vue CLI 3.2.1

devDependencies

"@vue/cli-plugin-babel": "^3.2.0",
"@vue/cli-plugin-eslint": "^3.2.0",
"@vue/cli-service": "^3.2.0",
"@vue/test-utils": "^1.0.0-beta.28",
"babel-eslint": "^10.0.1",
"babel-plugin-istanbul": "^5.1.0",
"cross-env": "^5.2.0",
"eslint": "^5.8.0",
"eslint-config-standard": "^12.0.0",
"eslint-loader": "^2.1.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^5.0.0-0",
"expect": "^24.0.0",
"istanbul-instrumenter-loader": "^3.0.1",
"jsdom": "^13.2.0",
"jsdom-global": "^3.0.2",
"mocha": "^5.2.0",
"mocha-webpack": "^1.1.0",
"node-sass": "^4.10.0",
"nyc": "^13.1.0",
"sass-loader": "^7.1.0",
"vue-template-compiler": "^2.5.17",
"webpack-cli": "^3.2.1",
"webpack-node-externals": "^1.7.2"

并且nyc中的package.json配置也如下:

"nyc": {
    "include": [
      "src/**/*.(js|vue)"
    ],
    "instrument": false,
    "sourceMap": false
  },

vue.config.js

const nodeExternals = require('webpack-node-externals')
const path = require('path')
let isCoverage = process.env.NODE_ENV === 'coverage';

module.exports = {
    mode: 'development',
    externals: [nodeExternals()],
    devtool: 'inline-cheap-module-source-map',
    output: {
        // use absolute paths in sourcemaps (important for debugging via IDE)
        devtoolModuleFilenameTemplate: '[absolute-resource-path]',
        devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            }
        ].concat(
            isCoverage ? {
                test: /\.(js|ts)/,
                include: path.resolve('src'),
                loader: 'istanbul-instrumenter-loader',
                options: { esModules: true }
            }: [],
            {
                test: /.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
            },
            {
                test: /\.ts$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'ts-loader'
            }
        )
    },
    target: 'node',
    externals: [nodeExternals()],
    devtool: "inline-cheap-module-source-map"
}

我的测试文件夹结构如下:

enter image description here

我的测试代码如下:

enter image description here

到目前为止,我尝试运行以下命令:

"test-ci": "mocha-webpack --webpack-config vue.config.js --require test/setup.js test/**/*.js"
"cover": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test-ci"

如果我运行 test-ci ,它将输出以下内容:WEBPACK Compiled successfully in 631ms 但是,如果我运行 cover 之后,它只是一个空白页,那么输出就没有结果了,nyc记录了所有内容:

enter image description here

我在这里做什么错了?

2 个答案:

答案 0 :(得分:0)

我面临着同样的问题,包括vue和js文件分别为我解决了这个问题。

"nyc": {
    "include": [
      "src/**/*.js",
      "src/**/*.vue"
    ],
    "instrument": false,
    "sourceMap": false
  },

答案 1 :(得分:0)

尽管使用节点v10.16.0和vue-cli 3.11.0,但我在这里遇到了同样的问题。 Mohit的答案无效,但以下语法有效:

// package.json
  "nyc": {
        "extension": [
            ".js",
            ".vue"
        ]
  },