gpt4 book ai didi

javascript - Gulp mocha Istanbul 尔 免费获得 100%

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:01 25 4
gpt4 key购买 nike

我使用以下代码,它能够按预期生成 Mocha 测试(套件和测试)报告,但我无法看到 Istanbul 尔的 Mocha 测试覆盖率我在这里错过了什么?

gulp.task('test', function () {
gulp.src(['./assets/**/js/*.js'])//Here I took it from the repo
.pipe(istanbul())
.pipe(tap(function (f) {
require(f.path);
}))
.on('end', function () {
gulp.src(['test/*spec.js'])//this is the path for my tests
.pipe(mocha({
reporter: 'mochawesome'
}))
.pipe(istanbul.writeReports(
['unit-test-coverage']
));
});
});

以下内容不会生成任何内容

  .pipe(istanbul.writeReports(
['unit-test-coverage']
));

我的项目如下所示

my app
-server.js
-app.js
-contoller
--file1.js
--file2.js
-test
spec1.js
spec2.js
-gulpfile.js
  1. 我应该在这里放什么?gulp.src(['./assets/**/js/*.js']) ?
  2. 我应该如何采用我的代码才能使其正常工作?

我使用以下 https://github.com/SBoudrias/gulp-istanbul喝一口 Mocha https://www.npmjs.com/package/gulp-mocha

这是代码运行后得到的结果

19 次通过(15 秒)

[mochawesome] Report saved to mochawesome-reports/mochawesome.html


----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|----------------|


=============================== Coverage summary ===============================
Statements : 100% ( 0/0 )
Branches : 100% ( 0/0 )
Functions : 100% ( 0/0 )
Lines : 100% ( 0/0 )
================================================================================

更新当我执行以下操作时,它会生成一份报告,但覆盖率很高,但似乎代码覆盖率不是很好,因为我得到了 100% 的所有测试(我希望:-)),知道我在这里错过了什么吗?由于 Mocha 单元测试运行良好,只是代码覆盖率效果不佳

            .pipe(istanbul.writeReports({
dir: './assets/unit-test-coverage',
reporters: ['html'],
reportOpts: {dir: './unit-test-coverage'}
}));

更新2

我在 GRUNT 中构建了相同的内容(因为我在这里迷路了:-( ),这正在工作!!!!!!

但我仍然想让它在 Gulp 中工作,请帮忙!

我尝试分离所有依赖项并创建这个小 gulp 文件并运行只是它,仍然是同样的问题

这是新文件,我使用不同的技术,但结果相同:(

var gulp = require('gulp');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-mocha');


gulp.task('pre-test', function () {

return gulp.src(['./assets/**/js/*.js'])
// Covering files

.pipe(istanbul(
{includeUntested: true}
))
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function () {
return gulp.src(['test/*spec.js'])
.pipe(mocha({
reporter: 'mochawesome'
}))
// Creating the reports after tests ran
.pipe(istanbul.writeReports({
dir: './assets/unit-test-coverage',
reporters: ['html'],
reportOpts: {dir: './unit-test-coverage'}
}))
.pipe(istanbul.enforceThresholds({thresholds: {global: 90}}));
});

gulp.task('default', [ 'test']);

我在这里缺少什么?测试文件运行正常,只是覆盖范围...

最佳答案

正如评论中所说。

您似乎没有 assets 文件夹。至少“我的项目如下”没有它。尝试一下

return gulp
// all js files from cwd excluding test folder and gulpfile
.src(['./**/*.js', '!test/', '!gulpfile.js'])
.pipe(istanbul(...))

关于javascript - Gulp mocha Istanbul 尔 免费获得 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com