简体   繁体   English

如何在运行mocha测试后杀死nodemon进程?

[英]How can I kill nodemon process after running mocha tests?

I need to have a gulp task that starts the server, runs mocha tests against it and finally closes it. 我需要有一个gulp任务启动服务器,对它运行mocha测试,最后关闭它。 I have the following code: 我有以下代码:

var mocha = require('gulp-mocha');
var nodemon = require('nodemon');

gulp.task('my-integration-tests', function () {    
  return nodemon({ script: './server.js' })
    .on('start', function () {
      gulp.src(['./mySpecs.spec.js'])
        .pipe(mocha());            
  });    
});

The server is successfully started and the tests are run. 服务器已成功启动并运行测试。 However after this the process created by nodemon is still alive. 但是在此之后, nodemon创建的进程仍然存在。 Is there a way to instruct nodemon to close upon completion? 有没有办法指示nodemon在完成后关闭? Also having the application opening and closing in the same process as the mocha tests is not an option with the current configuration. mocha测试的同一过程中打开和关闭应用程序也不是当前配置的选项。


UPDATE: 更新:

Apart from ThomasBromans answer, I came up with this solution which seems to work in my case. 除了ThomasBromans的答案之外,我想出了这个似乎适用于我的解决方案。 Whenever gulp-mocha finishes the tests it will kind of emit an 'end' event. 每当gulp-mocha完成测试时,它都会发出“结束”事件。 When this happens we only need to emit 'quit' on the child process then kill the main process, like so: 当发生这种情况时,我们只需要在子进程上发出'quit'然后终止主进程,如下所示:

gulp.task('my-integration-tests', function () {    
  var childProc = nodemon({ script: './server.js' });

  childProc.on('quit', function () {
    console.log('event emitted, child process is being killed');
  })

  childProc.on('start', function () {
      gulp.src(['./mySpecs.spec.js'])
        .pipe(mocha())
        .once('end', function () {
          console.log('mocha stuff ended. time to kill processes');
          childProc.emit('quit');
          setTimeout(function () {
            console.log('kill main process');
            process.exit();
          }, 1500);
        });            
  });    
});

Unfortunately I still need the timeout between the child process being killed and the killing of the main process, if I remove the timeout it happens that the child process remains hanging. 不幸的是,我仍然需要在子进程被杀死和杀死主进程之间的超时,如果我删除超时,它会发生子进程仍然挂起。 This solution is of course open to improvements. 该解决方案当然可以改进。

You can exit the process with process.exit() . 您可以使用process.exit()退出该过程。 Just add another .pipe . 只需添加另一个.pipe Your task will look like this: 您的任务将如下所示:

gulp.task('my-integration-tests', function () {    
  return nodemon({ script: './server.js' })
    .on('start', function () {
      gulp.src(['./mySpecs.spec.js'])
        .pipe(mocha())
        .pipe(process.exit());
  });
});

EDIT running tasks in a sequence (I am not sure this works without any changes): 编辑顺序运行任务(我不确定这是否可以正常运行):

var gulp = require('gulp'),
    mocha = require('gulp-mocha'),
    nodemon = require('nodemon'),
    runSequence = require('run-sequence');

gulp.task('nodemon', function() {
    return nodemon({script: './server.js'});
});

gulp.task('mocha', function() {
    return mocha();
});

gulp.task('stop', function() {
    process.exit();
});

gulp.task('my-integration-tests', function () {    
    runSequence('nodemon',
                'mocha',
                'stop');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM