supertest
supertest copied to clipboard
Error: ECONNREFUSED: Connection refused
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: '[email protected]',
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)
await request(app).post('api/v1/workspace').send(payload);
try adding a slash before API, maybe that's the issue
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.
This might be too obvious of a mistake, but I mocked the express app -- which also led to this error.