EADDRINUSE error when running node integration tests

I’m running my integration tests in the “13- Integration Testing” unit. Sometimes, when I run those tests, I get this error:

listen EADDRINUSE: address already in use :::3000

I’ve compared my solution to the code in the “after” directory, and I don’t see what I’m doing wrong. Can someone tell me how I can figure out what’s causing this problem?

Thanks for any help!


I have a theory about why I’m getting these errors:

  1. Within a single "describe"block, I have multiple tests.
  2. All tests within a single “describe” block run in parallel.
  3. That “describe” block also contains a “beforeEach” and an “afterEach” method.
  4. “beforeEach” starts my Node server.
  5. “afterEach” kills my Node server.

So, sometimes, I’ll have two tests that are both trying to start my Node server at the same time, thereby causing my EADDRINUSE error.

Is my theory correct? If so, why didn’t Mosh get this error when he ran his own tests?

Maybe Mosh avoided these errors because he was using an older version of jest? I’m using version 26.6.0.

OK, I’ve gotten a little further ahead in the lessons, and I see that Mosh avoids this error by awaiting each server.close(). But why would that work? It’s my understanding that while Jest runs the tests within each file sequentially, it runs the different test files in parallel. It seems like we’d still get two different test files trying to start the Node server at the same time.

So, what am I missing here?

ok guys here is the solution. GO TO PACKAGE.JSON, under scripts, test, you probably already have some flags like --verbose --watchAll, NOWWWWWW ADDDD another flag called --maxWorkers=1


I’m having the same issues, i don’t know why, but unless i use --runInBand or --maxWorkers=1. Even using --detectOpenHandles didn’t work because my tests ran perfecty when using that flag. So i’m getting really confused on why this problem happens to me but not to mosh (Even before fixing his ‘await server.close()’ he didn-t have any issues.

1 Like

“devDependencies”: {

"jest": "^26.6.3",

"supertest": "^6.0.1"

I got the same issue, and I tried adding --maxWorkers=1, or everything, but it still did not work.
Finally it worked when

I killed task mongodb 27017 , then started it again and connected it.

Then I added --maxWorkers=1 in scripts

“scripts”: {

"test": "jest --watchAll --verbose --coverage --maxWorkers=1"

I honestly dont know if the task kill helped.
just share what I did that it worked for me


You may need to comment out the “winston-mongodb” related codes in “./startup/logging.js”. Please see the followings:

const config = require('config');
const winston = require('winston');
// require('winston-mongodb');

module.exports = function () {
    new winston.transports.Console({
      colorize: true,
      prettyPrint: true
    new winston.transports.File({
      filename: 'uncaughtEx.log',
      json: false,
      timestamp: true,

  process.on('unhandledRejection', (ex) => {
    throw ex;

  winston.add(winston.transports.File, {
    filename: 'logfile.log',
    json: false,
    timestamp: true,
  // winston.add(winston.transports.MongoDB, { db: config.get('db'), level: 'error', });

This worked for me. I renamed the integration test files with “xxxx” instead of “test” eg auth.xxxx.js instead of auth.test.js so I could check each file independently. If I ran tests in genres.test.js by itself (other two files renamed) all tests passed. If I ran auth.test.js with other two files renamed then auth.test.js ran fine. But if I ran tests with both genres.test.js AND auth.test.js then it would throw an error related to the port being in use. Thats crazy! One set of tests shouldnt affect another!! If I run all three then I get two errors, both relating to Port. So there definitely is something going on with running multiple tests. Port is being opened in one test and then another test file tries to open same port. Anyway, I added --maxWorkers=1 and all three ran with no errors. Thanks for the solution.

This is what I did

  1. "test": "jest --watchAll --verbose --coverage --maxWorkers=1"
  2. comment out all the mongodb related codes in “./startup/logging.js”.

my current situation:

This solution works for me sometime and sometime still failed saying

listen EADDRINUSE: address already in use :::3000

this is so weird, anybody can help?