0

I am using MOCHA to test some express framework code.

I have written a simple MOCHA code to test messages returned in the response header. The code works. It also means that I am connected to the server and I can get the file from the database.

Now, I want to use "SuperTest" to do the same thing. But, I get "Error: connect ECONMREFUSED"

Here is my code:

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

var app = express();

describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
    this.timeout(15000);
    it ('Check header message', function(done) {
      request(app)
        .get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
        .expect('warning', '100 Max Record Limit Exceeded')
        .expect('Content-Type', /json/)
        .expect(200, done);
    } )
} )

and the error showing on the console is:

1) GET /core/dbq/534e930204dd311822ec1c9d Check header message:
   Error: connect ECONNREFUSED
   at errnoException (net.js:901:11)
   at Object.afterConnect [as oncomplete] (net.js:892:19)

I am learning to use "SuperTest". Please help. Thank you.

user2886680
  • 61
  • 3
  • 9

1 Answers1

0

Supertest starts the application on a random port and fills the host+port part of the URL for you. Your code should supply the path (and query) part only.

request(app)
    .get('/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
    // etc.
    .expect(200, done);

Alternatively, you can start the application yourself before running the test.

describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
  this.timeout(15000);
  before(function(done) {
    app.listen(3001, function() { done(); });
  });

  it ('Check header message', function(done) {
    request(app)
      .get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
      // etc.
  });
});

I would highly recommend going with the first approach, otherwise your tests may clash with other applications listening on port 3001.

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99