我需要一个
服务器已经成功启动并运行测试。但是在此之后,由nodemon创建的进程仍然存活着。有没有办法指示nodemon在完成后关闭?另外,在当前配置下,将应用程序打开和关闭到与mocha测试相同的进程中也不是一个选项。
更新: 除了ThomasBromans的答案外,我想出了这个解决方案,它似乎在我的情况下有效。每当gulp-mocha完成测试时,它会发出一种“end”事件。当发生这种情况时,我们只需要在子进程上发出“quit”,然后杀死主进程,像这样:
gulp
任务来启动服务器,在其上运行 mocha
测试,并最终关闭它。我有以下代码: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());
});
});
服务器已经成功启动并运行测试。但是在此之后,由nodemon创建的进程仍然存活着。有没有办法指示nodemon在完成后关闭?另外,在当前配置下,将应用程序打开和关闭到与mocha测试相同的进程中也不是一个选项。
更新: 除了ThomasBromans的答案外,我想出了这个解决方案,它似乎在我的情况下有效。每当gulp-mocha完成测试时,它会发出一种“end”事件。当发生这种情况时,我们只需要在子进程上发出“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);
});
});
});
很不幸,我仍然需要在子进程被杀死和主进程被杀死之间设置超时时间,如果我去掉超时时间,可能会导致子进程一直挂起。当然,这个解决方案还有改进的余地。