我正在尝试设置我的快递服务器的茉莉花测试。我正在使用每个规范启动一个新服务器,并在每个规范完成后尝试将其关闭。不幸的是,服务器似乎没有关闭......使得运行多个规范成为不可能。
server.js:
var app = require('express')();
exports.start = function(config){
if(!this.server){
app.get('/', function(req, res){
res.status(200);
res.end();
})
this.server = app.listen(config.port, function(){
console.log('Server running on port %d', config.port);
});
}
};
exports.close = function(){
this.server.close();
}
路由-spec.js:
var server = require('./path/to/server.js');
var http = require('http');
describe('express server', function () {
beforeEach(function () {
server.start({port: 8000});
});
afterEach(function () {
server.close();
});
describe('/', function () {
it('should return 200', function (done) {
http.get('http://localhost:8000', function (res) {
expect(res.statusCode).toBe(200);
done();
});
});
});
});
第一个规范按预期通过,但终端从未完成测试(即:服务器仍在运行),并且添加的任何后续测试都会导致抛出“ECONNREFUSED”。