Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: Error found: listen EADDRINUSE :::3000?


I'm currently facing a bug that I can't resolve and I have been struggling for some hours.
I'm using the following versions:
Node: 8.11.3
Express: 4.16.3
Jest: 22.2.2
Mongoose: 5.2.3

I'm trying to do some integration tests with jest and I have 2 files with tests.
In each file I wrote the following:
// Create the server before each test.
beforeEach(() => {
    server = require('../../index');
});

// Close the server after each test.
afterEach(async () => {
    if (server) {
        server.close();
    }
});

In index.js I have the following (This is not all the code, but the relevant code for the bug):

// Listen to the server.
const port = config.PORT || process.env.PORT || 3000;
module.exports = app.listen(port, () => {
   winston.info(`Listening to port ${port}...`);
});

When I run npm test I get this exception all the time:

**listen EADDRINUSE :::3000**

  10 | // Listen to the server
  11 | const port = config.PORT || process.env.PORT || 3000;
> 12 | module.exports = app.listen(port, () => {
  13 |     winston.info(`Listening to port ${port}...`);
  14 | });
  15 |

I tried several ways to solve this by adding async await to beforeEach and for afterEach and tried also to put the sever.close in afterAll and in beforeAll but still got the same error.

Then, I tried to solve by doing this:
How Do I Shut Down My Express Server Gracefully When Its Process Is Killed?

But again, with no luck.
Finally, when I wrote all the tests in 1 file, it works without this error. Does anybody know how to solve this? I don't want to write all my integration tests in 1 file..
Thanks!

like image 803
Or Assayag Avatar asked Dec 02 '22 11:12

Or Assayag


1 Answers

Try setting the --runInBand flag, this will run your tests sequentially.

https://jestjs.io/docs/en/cli#runinband

In package.json scripts:

"scripts": {
    ...
    "test": "jest --watchAll --verbose --runInBand",
    ...
}

[UPDATE] On further investigation, while you can use --runInBand to run your tests sequentially, another option is to await the server.close() since it returns a promise. As such, in your afterEach:

...
await server.close();
...

[UPDATE] I believe a better way to solve this issue is to separate out the index.js file into an index.js and a server.js file. This enables you to pull your index.js file into your tests without .listen:

// index.js
const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.status(200).send({ hello: "world!" });
});

module.exports = app;

Then in a separate server.js file:

const server = require("./index");

const PORT = process.env.PORT || 3001;

server.listen(PORT, () => {
  console.log(`App running on: ${PORT}`);
});

module.exports = server;

When running your app, you run: node server.js.

This then enables you to test your routes by pulling your index.js file into your test as follows:

// test.js
const request = require("supertest");
const server = require("./index");

describe("GET /", () => {

  test("should return status 200", async () => {
    const res = await request(server).get("/");
    expect(res.status).toBe(200);
  });


});

like image 85
Nicholas Pretorius Avatar answered Dec 23 '22 19:12

Nicholas Pretorius