Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: listen EADDRINUSE: address already in use :::3000 #568

Closed
ctfrancia opened this issue Mar 27, 2019 · 10 comments
Closed

Error: listen EADDRINUSE: address already in use :::3000 #568

ctfrancia opened this issue Mar 27, 2019 · 10 comments

Comments

@ctfrancia
Copy link

@ctfrancia ctfrancia commented Mar 27, 2019

Hey guys, so I have been just trying to start testing my Koa server using Supertest.

I am getting the error: Error: listen EADDRINUSE: address already in use :::3000

I have tried closing everything down on that port and run it but I'm still getting the issue.

Here is my app:

require('dotenv').config();
const Koa = require('koa');
const app = new Koa();
const router = require('./router');
const PORT = process.env.NODE_PORT || 3001;
const ENV = process.env.NODE_ENV || 'Development';
const logger = require('koa-logger');

app.use(logger());
app.use(router.routes());

app.listen(PORT, (err) => {
    if (err) console.error('❌ Unable to connect the server: ', err);
    console.log(`🌍 Server listening on port ${PORT} - ${ENV} environment`);
  });

    module.exports = app;

my router:

const Router = require('koa-router');
const router = new Router();

router.get('/', ctx => ctx.body = 'Sending some JSON');
  module.exports = router;

finally the test:

const server = require('../index');
const request = require('supertest').agent(server.listen());

afterEach(() => {
  server.close();
});

describe('routes: index', ()=> {
  it('should reach endpoint', async () => {
    const response = await request(server).get('/');
   
    expect(response.status).toEqual(200);
    expect(response.body).toEqual('Sending some JSON');
  });
});

my server isn't even running on port 3000 it is running on 8080
however, just to make sure I will run my server on 3000 and it starts up no problem. I then try to run the test, I get the error again with the server running on 3000 and the server not. Kinda stuck

@rimiti

This comment has been minimized.

Copy link
Collaborator

@rimiti rimiti commented Mar 27, 2019

Could you send us the output of:
lsof -i:3000

@ctfrancia

This comment has been minimized.

Copy link
Author

@ctfrancia ctfrancia commented Mar 27, 2019

Hi @rimiti I don't get any output

@ctfrancia

This comment has been minimized.

Copy link
Author

@ctfrancia ctfrancia commented Mar 27, 2019

UPDATE so the reason was because in my app.test.js file I had a series of tests that were not closing after completion because I was running tests like:

const app = require('../index');

describe('server working', () => {
  
  it('should exist', async (done)=> {
    expect(app).toBeDefined();
    done();
  });

  it('should be an object', async (done)=>{
    expect(typeof app).toBe("object");
    done();
  });
});

and I am still getting the error:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

@ctfrancia ctfrancia closed this Mar 27, 2019
@aech12

This comment has been minimized.

Copy link

@aech12 aech12 commented Jul 21, 2019

I had that exact error message, try this out:

Expected: I can use app.listen(PORT, ()=>{})

Code: index.js exports "app", app.listen(PORT, ()=>{}) is uncommented

Output: npm test => "Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue."

Workaround: comment/delete app.listen(PORT) and listen to the port on another file (example: require "app" from index.js in app.js and listen to a port in app.js)

@aech12

This comment has been minimized.

Copy link

@aech12 aech12 commented Jul 21, 2019

what's weird is that the documentation claims "if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports."

@ctfrancia

This comment has been minimized.

Copy link
Author

@ctfrancia ctfrancia commented Jul 22, 2019

Ok thanks a lot, I will be checking that out for sure!

@dgreene1

This comment has been minimized.

Copy link

@dgreene1 dgreene1 commented Aug 13, 2019

Looks like you have to do two things:

  1. ensure that the app has not yet been started (i.e. put the app.listen in an if statement that asserts it's not in test mode first)
  2. pass koa.callback() into supertest as follows:
request(app.callback())
@dionisoros

This comment has been minimized.

Copy link

@dionisoros dionisoros commented Nov 5, 2019

lsof -i:3000 
kill -9 [PID] 
@Atabic

This comment has been minimized.

Copy link

@Atabic Atabic commented Jan 9, 2020

You are actually running the main server in the terminal. Stop the server so the debugger can run on the same PORT

@spock123

This comment has been minimized.

Copy link

@spock123 spock123 commented Jan 19, 2020

So I had this issue as well.
What you want to do is that you don't want to start your server. In your main server express file you should export the server. But do NOT call the listen method.

So modify your code so that when you're testing (for example by setting an environment variable and looking for that), you're not calling the listen() method. This should resolve it and let you run your tests in parallel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.