ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
MIT License
13.75k stars 758 forks source link

Error: ECONNREFUSED: Connection refused #610

Open iamgkstack opened 4 years ago

iamgkstack commented 4 years ago

my app.js code is below:-

import express from 'express';
const app = express();
app.use(cors());
const session = require('express-session');
const http = require('http').createServer(app);
const io = require('socket.io').listen(http);
import config from './config/environment';
import initializeDB from './config/seed';
import mongoose from 'mongoose';
const connecString = config.getConstants().mongo.uri;
import fileUpload from 'express-fileupload';
import registerRoutes from './routes';
import cors from 'cors';
import { run_cron_job } from './config/helper';
import passport from 'passport';
require('./config/passport');

require('dotenv').config();

mongoose.connect(connecString, {
    useNewUrlParser: false
}).then(()=>{
    console.log("Blik database connected successfully.");
    initializeDB();
})
.catch((err)=>{
    console.log(err);
});

//Middlware
app.use(passport.initialize());
app.use(fileUpload());
app.use(express.json());

registerRoutes(app);

io.on('connection', function(socket){
    // console.log('A user connected');
    socket.on('disconnect', function(){
        // console.log('A user disconnected');
    });
});

export function experiment(){
    io.sockets.emit('new_token');
}

http.listen(config.getPort(),async ()=>{
    run_cron_job();
    console.log("Server is running on port " + config.getPort());
})
export default app;

my workspace.test.js file code is below:-

import { expect } from 'chai';
import request from 'supertest';
import app from '../../app';

describe('Workspace', async () => {

    it('should return 200 if workspace created sucessfully', async () => {
        const payload = {
            workspace_name: 'testing workspace',
            email: 'abc@gmail.com',
            mobile: '9721867247',
            name: 'abc',

        };

        const res = await request(app).post('api/v1/workspace').send(payload);
        // console.log('response', res);
        console.log('responseWorkspace =>', res);
    });

});

My package.json file is below:-

"scripts": {
    "start": "babel-node server/app.js",
    "prod": "babel-node server/app.js production",
    "test": "SET NODE_ENV=test&&mocha --require @babel/register --slow 100 --timeout 10000 server/**/*.test.js"
  }

Error code which I am getting

Server is running on port 3001
   Workspace
(node:20352) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
Blik database connected successfully.
    1) should return 200 if workspace created sucessfully

  0 passing (2s)
  1 failing

  1) Workspace
       should return 200 if workspace created sucessfully:
     Error: ECONNREFUSED: Connection refused
      at Test.assert (node_modules\supertest\lib\test.js:165:15)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1618:8)
      at process._tickCallback (internal/process/next_tick.js:63:19)
AvdiuAndin commented 4 years ago

await request(app).post('api/v1/workspace').send(payload);

try adding a slash before API, maybe that's the issue

kodikos commented 3 years ago

Also, if it helps anyone drifting in, the same ECONNREFUSED error comes up if you tried to request a fully-qualified URL from supertest. Just the path part is all you need.

VKBobyr commented 2 years ago

This might be too obvious of a mistake, but I mocked the express app -- which also led to this error.