首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用gulp-mocha显示测试覆盖率

如何使用gulp-mocha显示测试覆盖率
EN

Stack Overflow用户
提问于 2017-08-01 16:53:38
回答 1查看 1.6K关注 0票数 1

请房子,我已经将我的代码从es6转换为es5使用gulp作为一个任务运行器。我已经完成了与伊斯坦布尔的报道报告。在设置之后,它没有显示测试覆盖率。下面是我的代码

代码语言:javascript
复制
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
import path from 'path';
import mocha from 'gulp-mocha';
import exit from 'gulp-exit';
import coveralls from 'gulp-coveralls';
import cover from 'gulp-coverage';

将gulp插件加载到plugins变量中

代码语言:javascript
复制
const plugins = loadPlugins();

gulp.task('tests', () => {
  gulp.src('./server/tests/*.js')
    .pipe(plugins.babel())
    .pipe(mocha())
    .pipe(exit());
 });

将所有的Babel Javascript编译成ES5并放在dist文件夹中

代码语言:javascript
复制
const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**']
};

将所有的Babel Javascript编译成ES5并放入dist目录

代码语言:javascript
复制
gulp.task('babel', () =>
  gulp.src(paths.js, { base: '.' })
    .pipe(plugins.babel())
    .pipe(gulp.dest('dist'))
);

gulp.task('coverage', () => {
  gulp.src('server/test/**/*.js', { read: false })
    .pipe(cover.instrument({
     pattern: ['server/controllers/**/*.js'],
      debugDirectory: 'debug'
    }))
    .pipe(mocha())
    .pipe(cover.gather())
    .pipe(cover.format())
    .pipe(gulp.dest('reports'));
 });

gulp.task('coveralls', () => gulp.src('./coverage/lcov')
    .pipe(coveralls()));

使用对文件所做的每个更改重新启动服务器

代码语言:javascript
复制
  gulp.task('nodemon', ['babel'], () =>
    plugins.nodemon({
    script: path.join('dist', 'index.js'),
    ignore: ['README.md', 'node_modules/**/*.js', 'dist/**/*.js'],
    ext: 'js',
    tasks: ['babel']
  })
);

gulp.task('test', ['tests']);
gulp.task('default', ['nodemon']);
gulp.task('production', ['babel']);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-18 18:01:32

下面的代码片段演示了我如何通过一点修改来解决这个问题。

将以下代码放入您的gulpfile中

代码语言:javascript
复制
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
import path from 'path';
import shell from 'gulp-shell';

// Load the gulp plugins into the `plugins` variable
const plugins = loadPlugins();

// Compile all Babel Javascript into ES5 and place in dist folder
const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**', 
  '!./server/tests/**']
};

// Compile all Babel Javascript into ES5 and put it into the dist dir
gulp.task('babel', () =>
  gulp.src(paths.js, { base: '.' })
    .pipe(plugins.babel())
    .pipe(gulp.dest('dist'))
);

gulp.task('migrate', shell.task([
  'cross-env NODE_ENV=test sequelize db:migrate',
]));

gulp.task('coverage', shell.task([
  'cross-env NODE_ENV=test nyc mocha ./server/test/**/*.js',
]));

// Restart server with on every changes made to file
gulp.task('nodemon', ['babel'], () =>
  plugins.nodemon({
    script: path.join('dist', 'index.js'),
    ignore: ['README.md', 'node_modules/**/*.js', 'dist/**/*.js'],
    ext: 'js',
    tasks: ['babel']
  })
);

gulp.task('test', ['migrate', 'coverage']);
gulp.task('default', ['nodemon']);
gulp.task('production', ['babel']);

然后转到您的package.json,然后添加以下内容

代码语言:javascript
复制
"nyc": {
    "require": [
      "babel-register"
    ],
    "reporter": [
      "lcov",
      "text",
      "html"
    ],
    "sourceMap": false,
    "instrument": false,
    "exclude": [
      "the test file you want to exclude from coverage"
    ]
  }

绝对做到了

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45433235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档