Get the Mocha End Event from gulp

I am using gulp -mocha with gulp and I am trying to get notified when mocha tests are completed.

This is my 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'] );

I was able to see the notification of the "error" event, but I could not find a way to get the "end" event.

Any idea on how to get it?

+4
source share
1 answer

gulp-notifyworks with threads (i.e. pipe(notify...), use node-notifier.

I do it like this:

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'))
  ;
});
+2
source

Source: https://habr.com/ru/post/1624591/


All Articles