我为单元测试创建了gulp任务。我添加nodemon以自动运行服务器然后运行测试。但是再次运行gulp任务时会出错。我有错误说端口已经忙于另一个进程。
我使用此代码:
.slide-in-right {
-webkit-transform: translateX(100%);
transform: translateX(100%); }
.slide-in-right.ng-enter, .slide-in-right > .ng-enter {
-webkit-transition: all cubic-bezier(0.1, 0.7, 0.1, 1) 2000ms;
transition: all cubic-bezier(0.1, 0.7, 0.1, 1) 2000ms; }
.slide-in-right.ng-enter-active, .slide-in-right > .ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0); }
.slide-in-right.ng-leave, .slide-in-right > .ng-leave {
-webkit-transition: all linear 2800ms;
transition: all linear 2800ms; }
如何在单元测试后停止或终止服务器?
除了答案: 现在我有gulpfile.js:
var gulp = require('gulp'),
gulpUtil = require('gulp-util'),
gulpShell = require('gulp-shell'),
gulpEnv = require('gulp-env'),
gulpNodemon = require('gulp-nodemon'),
gulpMocha = require('gulp-mocha');
gulp.task('default', function () {
gulpUtil.log('unit - run unit tests');
});
gulp.task('server', function (callback) {
var started = false;
return gulpNodemon({
script: './build/app.js'
})
.on('start', function () {
if (!started) {
started = true;
return callback();
}
})
});
gulp.task('unit', ['server'], function () {
return gulp.src('./src/*.js')
.pipe(gulpMocha({reporter: 'spec'}))
.once('error', function () {
process.exit(1);
})
.once('end', function () {
process.exit();
})
});
同样在我的服务器脚本中,我添加了以下代码段:
var gulp = require('gulp'),
gulpUtil = require('gulp-util'),
gulpNodemon = require('gulp-nodemon'),
gulpMocha = require('gulp-mocha'),
gulpShell = require('gulp-shell');
var runSequence = require('run-sequence');
var nodemon;
gulp.task('default', function () {
gulpUtil.log('compile - compile server project');
gulpUtil.log('unit - run unit tests');
});
gulp.task('compile', function () {
return gulp.src('./app/main.ts')
.pipe(gulpShell([
'webpack'
]))
});
gulp.task('server', function (callback) {
nodemon = gulpNodemon({
script: './build/app.js'
})
.on('start', function () {
return callback();
})
.on('quit', function () {
})
.on('exit', function () {
process.exit();
});
return nodemon;
});
gulp.task('test', function () {
return gulp.src('./src/*.js')
.pipe(gulpMocha({reporter: 'spec'}))
.once('error', function () {
nodemon.emit('quit');
})
.once('end', function () {
nodemon.emit('quit');
});
});
gulp.task('unit', function() {
runSequence('compile', 'server', 'test');
});
因此,当测试结束并在服务器脚本中调用this.appListener = this.http.listen(process.env.PORT || 3000, '0.0.0.0', function() {
console.log(chalk.green("Server started with port " + _this.appListener.address().port));
});
// **Add**
function stopServer() {
console.log(chalk.cyan('Stop server'));
process.exit();
}
process.on('exit', stopServer.bind(this));
process.on('SIGINT', stopServer.bind(this));
时,我添加了事件 exit 事件处理程序,该服务器停止服务器并且gulp任务已成功完成停止服务器。
答案 0 :(得分:3)
Nodemon有一个quit
命令。请查看Using nodemon events并了解您的模块docs。根据您可以使用的文档:
var nodemon = require('nodemon');
// force a quit
nodemon.emit('quit');