2

I'm using node v6.2.0 and testing it via Mocha & Chai.

I wrote an api that when I test it with postman / website / node CLI, it works great, but when I use mocha to test it, i get an error that says:

{ [Error: connect ECONNREFUSED 127.0.0.1:4001] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 4001 }

Now, the thing is, that a prior test that tests the connection at http://localhost:4001 works fine...

This is the test code -

describe('/api/getAlbums', function () {
    this.timeout(5000);
    it('should get an array of 4 objects', function (done) {
        http.get('http://localhost:4001/api/getAlbums?uid=some_uid', function (res) {
            console.log(`Got response: ${res.statusCode}`);
           done();
        }).on('error', (e) => {
            console.log(`Got error: ${e.message}`);
            console.log('e', e);
            done();
        });
    });
});

And again, when i run the same function in the CLI (without the done();), it works fine.

I went over a lot of documentation but couldn't find anything, would love to get your assistance, Thank you.

alexunder
  • 2,075
  • 3
  • 16
  • 29
  • Hi You have to end the connection by res.end(); Please check the thread http://stackoverflow.com/questions/18867185/socket-hang-up-error-during-request for details. Do check the available answers before posting the questions. – Jeet May 31 '16 at 08:33
  • has nothing to do with it. If you were right, it wouldn't have worked using postman/cli/web – alexunder May 31 '16 at 08:37

1 Answers1

2

The source of the problem was nodemon. Looks like there was a synching issue. I've added the 'tests' folder to nodemon.json's ignore and it all works fine now.

alexunder
  • 2,075
  • 3
  • 16
  • 29