从Gulp获取摩卡“结束”事件

bvuwiixz  于 2022-12-16  发布在  Gulp
关注(0)|答案(1)|浏览(114)

我正在使用gulp-mocha与gulp,我试图得到一个通知时,摩卡测试完成。
这是我的gulpfile.js

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var notify = require("gulp-notify");
 
gulp.task('test', function () {
  return gulp.src(['test/**/*.js'], { read: false })
    .pipe( mocha({ reporter: 'nyan' }))
    .on("error", notify.onError({
      message: 'Error: <%= error.message %>'
    }))
    .on('end', function () {
      notify( "Tests passed" )
    });
});
 
gulp.task('watch-test', function () {
  gulp.watch(['./**'], ['test']);
});

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

我设法看到了关于“error”事件的通知,但是我找不到获取“end”事件的方法。
你知道怎么拿到吗?

hjzp0vay

hjzp0vay1#

gulp-notify适用于流(即pipe(notify ...),请使用node-notifier
我是这样做的:

let gulp     = require('gulp');
let gulpeach = require('gulp-foreach');
let mocha    = require('gulp-mocha');
let notifier = require('node-notifier');

// Run tests.
gulp.task('test', function () {
  gulp.src(['test/test-*.js'])
    .pipe(gulpeach(function (stream, file) {
      return stream
        .pipe(mocha())
        .on('error', (error) => {
          notifier.notify({
            message: ('Command: ' + error.cmd),
          })
        })
      ;
    }))
    .on('finish', () => notifier.notify('Tests completed'))
  ;
});

相关问题