# EADDRINUSE error when running node integration tests

**URL:** <https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364>\
**Category:** Node\
**Created:** [October 29, 2020, 10:32pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364 "2020-10-29T22:32:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![acohen1644](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@acohen1644](https://forum.codewithmosh.com/u/acohen1644)\
**Post date:** [October 29, 2020, 10:32pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/1 "2020-10-29T22:32:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![acohen1644](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@acohen1644](https://forum.codewithmosh.com/u/acohen1644)\
**Post date:** [October 29, 2020, 11:01pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/2 "2020-10-29T23:01:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![acohen1644](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@acohen1644](https://forum.codewithmosh.com/u/acohen1644)\
**Post date:** [October 29, 2020, 11:09pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/3 "2020-10-29T23:09:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![acohen1644](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@acohen1644](https://forum.codewithmosh.com/u/acohen1644)\
**Post date:** [October 31, 2020, 11:18pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/4 "2020-10-31T23:18:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ishakmohmed](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@ishakmohmed](https://forum.codewithmosh.com/u/ishakmohmed)\
**Post date:** [November 10, 2020, 8:19am UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/5 "2020-11-10T08:19:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FakeSun](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/fakesun/32/44_2.png) [@FakeSun](https://forum.codewithmosh.com/u/FakeSun)\
**Post date:** [November 26, 2020, 12:57am UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/6 "2020-11-26T00:57:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leealvin3](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@leealvin3](https://forum.codewithmosh.com/u/leealvin3)\
**Post date:** [January 4, 2021, 11:37pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/7 "2021-01-04T23:37:25Z")

</div>

“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

---

<div class="post-metadata">

**Author:** ![kurukuru](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/kurukuru/32/2824_2.png) [@kurukuru](https://forum.codewithmosh.com/u/kurukuru)\
**Post date:** [January 29, 2022, 9:51pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/8 "2022-01-29T21:51:21Z")

</div>

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

```auto
const config = require('config');
const winston = require('winston');
// require('winston-mongodb');
require('express-async-errors');

module.exports = function () {
  winston.handleExceptions(
    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', });
};

```

---

<div class="post-metadata">

**Author:** ![kavfam](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kavfam](https://forum.codewithmosh.com/u/kavfam)\
**Post date:** [March 30, 2022, 3:27pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/9 "2022-03-30T15:27:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qiqiliang](https://avatars.discourse-cdn.com/v4/letter/q/3ab097/32.png) [@qiqiliang](https://forum.codewithmosh.com/u/qiqiliang)\
**Post date:** [July 19, 2022, 4:17pm UTC](https://forum.codewithmosh.com/t/eaddrinuse-error-when-running-node-integration-tests/364/10 "2022-07-19T16:17:02Z")

</div>

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

```auto
listen EADDRINUSE: address already in use :::3000

```

this is so weird, anybody can help?
