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

问题描述 投票:1回答:1

请回家,我已经将我的代码从es6转换为es5,使用gulp作为任务运行器。我和伊斯坦布尔做了我的报道。设置后未显示测试覆盖率。下面是我的代码

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变量中

const plugins = loadPlugins();

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

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

const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**']
};

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

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()));

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

  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']);
javascript istanbul gulp-mocha
1个回答
1
投票

以下片段介绍了我如何通过一些修改解决了这个问题。

把以下代码放在你的gulpfile中

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然后添加以下内容

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

绝对完成

© www.soinside.com 2019 - 2024. All rights reserved.