question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught Error: ECONNREFUSED: Connection refused

See original GitHub issue

So I’m writing some tests using Mocha, chai and supertest.

I’m facing a really weird behavior here on a test that should work just fine. Here’s the test code:

var app = require('../app/app');
var agent = request.agent(app);

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

//Run request tests
describe('Request Tests - Setting Enviroment', () => {


    before((done) => {
        //set TEST env
        process.env.TEST = true;
        //load app
        app.start().then(() => {
            done();
        });
    });

    describe('basic page request', () => {
    
        it('should respond with 200 respond code', () => {

            request(app).get('/login')
            .expect(200)
            .expect('Content-Type', 'text/html; charset=utf-8')
            .end((err, res) => {
                if (err) throw err;
            });

        });

    });

    //Server ready
    describe('Fuzz Test', () => {

        describe('fuzzing login page with 1000 username/password permutations', () => {

            fuzzer.seed(0);

            it('should respond with 403 / invalid csrf token', () => {

                for(var i=0; i <= 1000; i++){

                    request(app).post('/login')
                    .send({
                        username: fuzzer.mutate.string('fuzzfromhere'),
                        password: fuzzer.mutate.string('fuzzfromhere')
                    })
                    .expect((code) => {
                        if (code != 403 && code != 429) throw code;
                    })
                    .end((err, res) => {
                        if (err) throw err;
                    });

                }

            });

        });

        describe('fuzzing tokenizer page with 1000 random values', () => {

            it('should respond with response from External API or invalid value', () => {
                // touch env to skip login and rate limiter
                process.env.TEST = 'skipLogin,skipRateLimiter';
                //get csrf to validate queries
                request(app).get('/tokenize')
                .expect((response) => {
                    console.log(`expect resp: ${response}`);
                })
                .end((err, res) => {
                    if (err) throw err;
                    console.error(`expect error: ${err}`);
                });
            });

        });
    
    });

    //Tests completed, end server
    after((done) => {
        app.end().then(() => {
            delete process.env.TEST;
            done();
        }).catch((err) => {
            throw err;
        });

    });

});

For some weird reason, I am getting the following error:

    Request Tests - Setting Enviroment
    Fuzz Test
    fuzzing tokenizer page with 1000 random cards
    should respond with response from Key Manager or invalid number:
    Uncaught Error: ECONNREFUSED: Connection refused
    at Test.assert (node_modules\supertest\lib\test.js:165:15)
    at assert (node_modules\supertest\lib\test.js:131:12)
    at C:\Users...\src\node_modules\supertest\lib\test.js:128:5
    at Test.Request.callback (node_modules\superagent\lib\node\index.js:718:3)
    at ClientRequest.req.once.err (node_modules\superagent\lib\node\index.js:646:10)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Any idea what’s causing this?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:16
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

80reactions
sabyannacommented, Mar 9, 2021

I had the same error message, my bug was an incorrect url, i forgot the ‘/’ at the beginning of the url.

0reactions
vlad-at-workcommented, Jul 3, 2022

Is it possible supertest to be the cause of the issue?

Error: ECONNREFUSED: Connection refused

Reproduce

  • Mac OS 10.15.7
  • node 18.3.0
  • chai@4.3.6
  • supertest@6.2.3
var request = require('supertest');
var { expect } = require('chai');

var api = request('http://127.0.0.1:4000'); // <-- works
var api = request('http://localhost:4000'); // <-- does not work

$ curl http://127.0.0.1:4000/home Welcome Home

What I found was that you have to import express app and pass it to request(app) like so, otherwise you’d get the ECONN problem. I’m not totally sure why this is but I hope this helps someone.

Also, don’t forget to then return the result of the (likely await) in the test function or else the test suite will complain at you: either return the promise or have a done(). Such were my meager findings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught Error: ECONNREFUSED: Connection refused #484
So I'm writing some tests using Mocha, chai and supertest. I'm facing a really weird behavior here on a test that should work...
Read more >
Node Mocha & Supertest: Error - Connection refused
I have built an api and i want to test some of the endpoints. I have a number of tests that are similar...
Read more >
How to Fix ECONNREFUSED – connection refused by server ...
One of the possible reasons for this error is that the firewall and anti-virus software on your computer is preventing FileZilla from making...
Read more >
ECONNREFUSED - Connection refused by the server
This error indicates that the user is trying to connect to your server and is unable to connect to the port. There are...
Read more >
Step-by-Step Fix "ECONNREFUSED - Connection refused by ...
In this guide we show you how to resolve FileZilla's ECONNREFUSED - Connection refused by server error along with tips and tools that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found