Closing an express server after running jasmine specs
Asked Answered
E

2

4

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.

Evidentiary answered 22/4, 2014 at 21:7 Comment(0)
W
2

You can use the npm module server-destroy. server-destroy keeps track of all connections and then closes them when destroy is called. Also, the destroy method takes a callback so you should pass your "done" function to your destroy method... Below is copied from the npm module description with the addition of a callback in the destroy call.

var enableDestroy = require('server-destroy');

var server = http.createServer(function(req, res) {
  // do stuff, blah blah blah
});

server.listen(PORT);

// enhance with a 'destroy' function
enableDestroy(server);

// some time later...
server.destroy(done);

If open connections are not a problem you can simply pass the done function to the close function, server.close(done) as described in this post How to correctly unit test Express server

Wellborn answered 6/12, 2016 at 18:32 Comment(0)
B
1

I've never been able to reliably shut Express programmatically. Instead, I recommend using something like jasmine-before-all to start the server once for all of your integration tests. This also speeds up your tests.

One of the nice things about mocha is that it includes a root suite before and after, which is useful for just this sort of thing. See https://mcmap.net/q/1633252/-two-files-using-supertest-with-mocha-causing-eaddrinuse

Birthroot answered 23/4, 2014 at 15:22 Comment(1)
Interestingly enough, I switched over to mocha because of the after feature... but that too didn't shut down the server. So I tried supertest with mocha and, finally, was able to get the testing behavior I was looking for.Evidentiary

© 2022 - 2024 — McMap. All rights reserved.