title: grunt testing api with supertest, express and mocha
body: <p>I have an https server running by express, which i test using mocha and supertest.</p>
  
  <p>My problem is - if i run only the test - its ok. If i try to run gruntfile with test then run express - i see lot of EADDRINUSE errors, even if in test files i do after() with app.close().
  Same applies to watch task on tests.</p>
  
  <p>Here is my exapmle test:</p>
  
  <pre><code>/* jshint node: true*/
  /*global describe, it, after*/
  (function() {
      'use strict';
      process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
      var request = require('supertest');
      var app = require('../server.js').app;
      var expect = require('chai').expect;
      var Cookies;
  
      after(function(done) {
          app.close();
          setTimeout(function(){done();}, 1500);
      });
  
      describe('/login', function() {
          it('should auth the user', function(done) {
              request(app)
                  .post('/login')
                  .send({login: "test", password: 'test'})
                  .expect(302)
                  .end(function(err, res) {
                      expect(err).to.be.equal(null);
                      expect(res.text).to.be.equal("Moved Temporarily. Redirecting to /");
                      Cookies = res.headers['set-cookie'].pop().split(';')[0];
                      done();
              });
  
          });
      });
      // testing API for serving session data for angualar factory
      describe('/api/session', function() {
          it('should return session data in JSON', function(done) {
              var req = request(app).get('/api/session');             
                  req.cookies = Cookies;
                  req.set('Accept','application/json')
                  .end(function(err, res) {
                      expect(err).to.be.equal(null);
                      expect(res.body).to.have.property("_id");
                      done();
              });
          });
      });
  
  }());
  </code></pre>
  
  <p>Im aware that those test are far from perfect. Im just beginning my adventure with proper software testing.</p>
  
  <p>All those "port already in use" are obvious, and none of them... give any issue. all tests work fine, server works fine, but stdout is crazy. Such behaviour is far from ideal, and probably full of potential problems and unstability issues.</p>
  
  <p><strong>My question is - how to get rid of it?</strong></p>
  
  <p><strong>My ideas are:</strong></p>
  
  <ul>
  <li><p>create dedicated server for test on just different port. Unfortunately i have to clue how to implement this.</p></li>
  <li><p>make some conditional for superagent to run server if it is not running, or just... pass it to superagent otherwise?</p></li>
  <li><p>use something else then superagent (like request, but im not sure if all the cookies and node_tls_reject_unauthorized would work.</p></li>
  </ul>
  
  <p>As You can see - i struggle with that topic and have more questions than answers, and not enough experiance to know where to look at.</p>
  
  <p>I kindly appreciate any help.</p>
  
  <p>EDIT:</p>
  
  <p>I found, that i can do:</p>
  
  <pre><code>before(function(done) {
      app.listen(3001, function() { done(); });
  });
  </code></pre>
  
  <p>which starts test on another port but... the whole server.js is loaded anyway, so it starts anyway too. Then, when firing it together with running server, there is obvious EADDRINUSE.</p>