Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: listen EADDRINUSE: address already in use :::3000 when mocha unit testing

I'm getting this error when unit testing code,

2 passing (14ms) 1 failing

1) Uncaught error outside test suite: Uncaught Error: listen EADDRINUSE: address already in use :::3000 at Server.setupListenHandle [as _listen2] (net.js:1255:14) at listenInCluster (net.js:1303:12) at Server.listen (net.js:1391:7) at Function.listen (node_modules/express/lib/application.js:618:24) at Object.listen (main.js:39:5) at Module._compile (internal/modules/cjs/loader.js:721:30) at Module._compile (node_modules/pirates/lib/index.js:99:24) at Module._extensions..js (internal/modules/cjs/loader.js:732:10) at Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7) at Module.load (internal/modules/cjs/loader.js:620:32) at tryModuleLoad (internal/modules/cjs/loader.js:560:12) at Function.Module._load (internal/modules/cjs/loader.js:552:3) at Module.require (internal/modules/cjs/loader.js:657:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (test/main.test.js:4:1) at Module._compile (internal/modules/cjs/loader.js:721:30) at Module._compile (node_modules/pirates/lib/index.js:99:24) at Module._extensions..js (internal/modules/cjs/loader.js:732:10) at Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7) at Module.load (internal/modules/cjs/loader.js:620:32) at tryModuleLoad (internal/modules/cjs/loader.js:560:12) at Function.Module._load (internal/modules/cjs/loader.js:552:3) at Module.require (internal/modules/cjs/loader.js:657:17) at require (internal/modules/cjs/helpers.js:22:18) at Array.forEach () at StatWatcher.onchange (internal/fs/watchers.js:50:8)

I'm using mocha, and i have --watch on my package.json. I'm using an es6 approach to express.

Package.json

{
  "name": "elies6express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "./node_modules/.bin/mocha --watch --require @babel/register",
    "start": "nodemon --exec babel-node main.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "bookshelf": "^0.14.2",
    "chai-http": "^4.3.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^8.0.0",
    "express": "^4.17.0",
    "knex": "^0.16.5",
    "morgan": "^1.9.1",
    "path": "^0.12.7",
    "pg": "^7.11.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/register": "^7.4.4",
    "chai": "^4.2.0",
    "mocha": "^6.1.4",
    "nodemon": "^1.19.0",
    "reify": "^0.19.1",
    "request": "^2.88.0"
  }
}

main.js

import 'dotenv/config';
import cors from 'cors';
import express from 'express';
import logger from 'morgan';
import path from 'path';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import userRoute from './routes/users';

const app = express();

app.use(cors());
app.use(logger('dev'));
// For React Stuff if need be
// app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'build')));
app.use(cookieParser());
app.use(bodyParser.json());

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.use('/users', userRoute);

app.use(() => (req, res, next)  =>{
  res.locals.user = req.user; // This is the important line
  // req.session.user = user
  console.log(res.locals.user);
  next();
});
app.use(bodyParser.urlencoded({ extended:false})); 

//build mode
// app.get('*', (req, res) => {
//   res.sendFile(path.join(__dirname+'/client/public/index.html'));
// })

app.listen(process.env.PORT, () =>
  console.log(`Example app listening on port ${process.env.PORT}!`),
);

export default app;

main.test.js

import chai from "chai"
import chaiHttp from 'chai-http';
import request from 'request';
import server from '../main';
const expect = chai.expect;
const should = chai.should();


chai.use(chaiHttp);

// should get / 

describe('should GET /',  () => {
    it('should get 200 status', (done) =>{
        chai.request(server)
        .get('/')
        .end( (err, res) => {
            res.should.have.status(200);
            done();
        });
    });

})
// should check for Hello World!
describe('Should check for Hello World! text', () => {
    it('should check for hello world text', (done) =>{
        chai.request(server)
        .get('/')
        .end( (err, res) => {
            expect(res.body).to.be.an('object') // works
            expect(res.text).to.equal('Hello World!')  // use res.text to check for res.send() text
            done();
        })
    })

})
like image 794
randal Avatar asked Oct 21 '25 04:10

randal


1 Answers

In main.js, place your app.listen call inside a test for !module.parent, like this:

if(!module.parent){
  app.listen(process.env.PORT, () =>
    console.log(`Example app listening on port ${process.env.PORT}!`),
  );
}

Source: http://www.marcusoft.net/2015/10/eaddrinuse-when-watching-tests-with-mocha-and-supertest.html

like image 103
LexH Avatar answered Oct 22 '25 18:10

LexH