Express: npm test returns "connect ECONNREFUSED"

472 Views Asked by At

I'm trying to test my app, and it always returns an error of connect ECONNREFUSED. I made a simple example to show what's happening. Here's my controller (CompoundJS code):

load('application');

action('test', function() {
  var obj = {success: true, data: 'blah'};
  send(obj);
});

action(function show(data) {
  var http = require('http');
  var options = {
    path: '/getTest',
    port: process.env.PORT  // without this, http fails because default port is 80
  };
  var req = http.get(options, function(res) {
    var data = '';
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      data = JSON.parse(data);
      return send(data);
    });
  });
  req.on('error', function(e) {
    return send({success: false, data: e.message}); // returns "connect ECONNREFUSED"
  });
});

So when I have the app running, I can hit /test (which is the show method there) and /getTest just fine without any errors. However, when I try to run the following test code, I get the error as stated above, and the issue comes down to that http.get, as I can get into the show function just fine.

var app, compound
  , request = require('supertest')
  , sinon = require('sinon');

function TestStub() {
  return {
  };
}

describe('TestController', function() {
  beforeEach(function(done) {
    app = getApp();
    compound = app.compound;
    compound.on('ready', function() {
      done();
    });
  });

  /*
   * GET /tests
   * Should render tests/index.ejs
   */
  it('should render "index" template on GET /tests', function(done) {
    request(app)
      .get('/test')
      .end(function(err, res) {
      console.log(res.body);
      done();
    });
  });

});

Any ideas on how to fix this? Cross posted from the CompoundJS Google Group.

0

There are 0 best solutions below