I am trying to set up jasmine testing of my express server. I am spinning up a new server with each spec and trying to shut it down after each spec completes. Unfortunately, the server doesn't seem to be shutting down... making running more than one spec impossible.
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();
}
routing-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();
});
});
});
});
The first spec passes as expected but the terminal never completes the test(ie: the server is still running) and any subsequent tests added cause a "ECONNREFUSED" to be thrown.