如何为mocha-webpack分离文件

时间:2018-07-24 00:59:09

标签: javascript webpack mocha mocha-webpack

正在发生的事情是,当我运行测试时,我的覆盖范围仅显示bundle.js,对您没有帮助。

我具有以下webpack文件设置,并想知道应该进行哪些更改以使每个文件被单独覆盖

webpack.config-test.js

var nodeExternals = require("webpack-node-externals")
const path = require("path")

module.exports = {
    context: path.resolve(__dirname),
    resolve: {
        extensions: [".js"],
        alias: {
            "@": path.join(__dirname, "../../src/server"),
        }
    },
    output: {
        path: "./",
        filename: "[name].js",
    },    
    target: "node", // webpack should compile node compatible code
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
}

通过npm运行命令:

nyc mocha-webpack --webpack-config test/server/webpack.config-test.js --glob \"*spec.js\" test/server/unit



当前输出为:

All files  |    90.38 |    70.83 |    90.91 |     90.2 |                   |
 bundle.js |    90.38 |    70.83 |    90.91 |     90.2 |... 78,280,282,306

我期望输出是

All files     |       80 |    68.75 |       80 |       80 |                   |
 functions.js |    78.79 |    68.75 |       80 |    78.79 |... 59,60,62,64,88 |
 mixin.js     |      100 |      100 |      100 |      100 | 

在非mocha-webpack版本中,我还为每个测试添加了文件名,我希望在webpack版本中也能做到这一点。因此没有 Webpack,我在index.js上运行,即

index.js

const fs = require("fs")
const path = require("path")

const files = fs.readdirSync(__dirname)
files.forEach(file => 
{
    if (!file.match(/\.spec\.js$/))
        return

    console.log(file)

    describe(file, function () 
    {
        require(path.join(__dirname, file))
    })
})

然后输出类似:

  sql.spec.js
    Some SQL tests
      ✓ should be 10

  test.spec.js
    generateRandomString
      ✓ should generate a 20 length string
      ✓ should generate a 40 length string
      ✓ should throw error for -10
      ✓ should throw error for length
    getRequiredProps
      ✓ should get prop
      ✓ should throw error
    toTime
      ✓ 1 seconds should return 1000
      ✓ 1 minutes should return 60000
      ✓ 1 hours should return 3600000
      ✓ 1 days should return 86400000

更新

有源映射,但显示的内容比我想要的多得多:https://github.com/zinserjan/mocha-webpack/blob/master/docs/installation/webpack-configuration.md

------------------------------------------|----------|----------|----------|----------|-------------------|
File                                      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------------------------------|----------|----------|----------|----------|-------------------|
All files                                 |     78.8 |    54.72 |    87.27 |     78.7 |                   |
 .tmp/mocha-webpack/1532582562486/webpack |    95.45 |       75 |    83.33 |    95.24 |                   |
  bootstrap 4e654663ecc955703de0          |    95.45 |       75 |    83.33 |    95.24 |                49 |
 node_modules/mocha-webpack/lib           |      100 |      100 |      100 |      100 |                   |
  entry.js                                |      100 |      100 |      100 |      100 |                   |
 src/server                               |       64 |    48.65 |       70 |       64 |                   |
  db.js                                   |    45.61 |    26.32 |    45.45 |    45.61 |... 20,122,126,138 |
  functions.js                            |    84.85 |    72.22 |      100 |    84.85 |    42,58,59,60,87 |
  mixin.js                                |      100 |      100 |      100 |      100 |                   |
  mock.js                                 |      100 |      100 |      100 |      100 |                   |
 src/server/post                          |       75 |     62.5 |      100 |       75 |                   |
  maptool.js                              |       75 |     62.5 |      100 |       75 |... 41,148,158,159 |
 test/server/unit                         |    98.33 |      100 |      100 |    98.33 |                   |
  functions.spec.js                       |    96.97 |      100 |      100 |    96.97 |                67 |
  maptool.spec.js                         |      100 |      100 |      100 |      100 |                   |
  mock.spec.js                            |      100 |      100 |      100 |      100 |                   |
  sql.spec.js                             |      100 |      100 |      100 |      100 |                   |
------------------------------------------|----------|----------|----------|----------|-------------------|

如何减少它,以便仅输出要检查的文件?

1 个答案:

答案 0 :(得分:3)

  

如何减少它,以便仅输出要检查的文件?

似乎您的spec文件也没有从覆盖率报告中排除。 尝试提供指向nyc配置中包含在覆盖率报告中的文件的路径。在您的package.json中:

{
  ...
  "nyc": {
    "include": [
      "src/server/**/*.js"
    ],
    ...
   },
   ...
}

如果尝试使用instanbul-instrumenter-loader来检测代码无济于事,请在Webpack配置中提供要在加载程序规则中包含的文件的路径:

    module: {
      rules: [
        {
          test: /\.js$/,
          use: { loader: 'istanbul-instrumenter-loader' },
          include: path.resolve('../../src/server')
        }
      ]
}

nyc的默认检测器应通过instrument: false配置中的nyc禁用。

更新

作为另一种解决方案,如果您使用的是Babel(和Vue),则可以使用babel-plugin-istanbul来执行代码,它会创建正确的映射(通过这种方式,我无法使istanbul-instrumenter-loader正确使用babel webpack配置)。

即使您没有Vue依赖关系但使用Babel,我也建议您查看vue-test-utils-mocha-webpack-example

希望有帮助。

相关资源:Code coverage for mocha-webpack