Home » Nodejs » Run only one mocha test file on change with gulp

Run only one mocha test file on change with gulp

Posted by: admin January 30, 2018 Leave a comment

Questions:

I have a standard gulp test watcher and runner for my Node.js/mocha setup;

gulp.task('mocha', function() {
  process.env.NODE_ENV = 'test';

  return gulp.src(['./test/**/*.js'], {read: false})
    .pipe(mocha({
      recursive: true, 
      reporter: 'list', 
      ui: 'bdd'
    })).on('error', gutil.log); 
});

gulp.task('tests', function() {
  gulp.watch(sources.concat(tests), ['mocha']); 
});

I’m wondering how to adapt my watch task (and the other) to only send to mocha the test that has changed – so that I don’t have to wait 20 seconds for a hundred tests to run.

I’ve tried this with fgulp-newer and gulp-cached, but no luck.

I’m looking here for an answer or for a pseudo-algorithm for a gulp plugin I could then write for this purpose 🙂

Answers:

You can use in your mocha test it.only() to run only one test

describe.only() is available too