Question

I am using express.js version 2.5.8 (from legacy code) and am looking to test route loading using supertest. I am having an issue with the server running, but not stopping. I run my tests using jasmine-node, which indicates that my assertion succeeds. However, the console shows that the process is still running.

var request = require('supertest')
  , express = require('express');

describe('Example Test', function() {
  var app;

  beforeEach(function() {
    app = express.createServer();

    app.configure(function() {
      app.set('port', process.env.PORT || 3000);
      app.use(express.logger('dev'));
      app.use(express.bodyParser());
      app.use(express.methodOverride());
    });

    app.get('/', function(req, res){
      res.send(200);
    });
  });

  it('index should return 200', function(done) {
    request(app)
    .get('/')
    .expect(200)
    .end(function(err, res) {
      expect(err).toBe(null);
      done();
    });
  });
});

This example is adapted from one likely using express.js 3.x.x. My assumption is that the express server behaves differently, leading to it not closing when the request terminates inside the test. I am uncertain how to correct this issue.

Was it helpful?

Solution

The server is still running because you aren't closing it anywhere. Just add app.close() to your end(function(){}) to stop the express server. If you also want to exit from node use process.exit().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top