如何为 mocha-webpack 分离文件
how to separate files for 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 | |
------------------------------------------|----------|----------|----------|----------|-------------------|
如何减少它以便只输出正在检查的文件?
How would I reduce it so that only the files being checked are outputted?
看起来甚至您的 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
默认仪器应该在 nyc
配置中使用 instrument: false
禁用。
更新
作为另一种解决方案,如果您使用 Babel(和 Vue),使用 babel-plugin-istanbul
检测代码是有意义的,它会创建正确的映射(顺便说一句,我无法使 istanbul-instrumenter-loader
与 babel webpack 一起工作配置正确)。
我建议看看 vue-test-utils-mocha-webpack-example 即使你没有 Vue 依赖但使用 Babel。
希望对您有所帮助。
发生的事情是,当我 运行 我的测试时,我的覆盖率只显示 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 | |
------------------------------------------|----------|----------|----------|----------|-------------------|
如何减少它以便只输出正在检查的文件?
How would I reduce it so that only the files being checked are outputted?
看起来甚至您的 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
默认仪器应该在 nyc
配置中使用 instrument: false
禁用。
更新
作为另一种解决方案,如果您使用 Babel(和 Vue),使用 babel-plugin-istanbul
检测代码是有意义的,它会创建正确的映射(顺便说一句,我无法使 istanbul-instrumenter-loader
与 babel webpack 一起工作配置正确)。
我建议看看 vue-test-utils-mocha-webpack-example 即使你没有 Vue 依赖但使用 Babel。
希望对您有所帮助。