I am starting making unit tests to my restapi using chai but I am getting Uncaught Error from console. Testing manually is working fine but I dont understand why is giving me this errors when I run the unit test.
index.js
import express from 'express';
import ip from 'ip';
import dotenv from 'dotenv';
import cors from 'cors';
import Response from './domain/response.js';
import HttpStatus from './config/http.config.js';
import artistRoutes from './route/artist.route.js';
import albumRoutes from './route/album.route.js';
import trackRoutes from './route/track.route.js';
import albumxtrackRoutes from './route/albumxtrack.route.js';
import artistxtrackRoutes from './route/artistxtrack.route.js';
import logger from './util/logger.js';
dotenv.config();
const PORT = process.env.APP_PORT || 3050;
const app = express();
app.use('/artists', artistRoutes);
app.use('/albums', albumRoutes;
app.use('/songs', trackRoutes);
app.use('/albums/relation/', albumxtrackRoutes);
app.use('/artists/relation/', artistxtrackRoutes);
app.get('/', (req, res) => res.send(new Response(HttpStatus.OK.code, HttpStatus.OK.status, `Basic request message!`)));
app.all('*', (req, res) => res.status(HttpStatus.NOT_FOUND.code)
.send(new Response(HttpStatus.NOT_FOUND.code, HttpStatus.NOT_FOUND.status, `Not existing route`)));
var server = app.listen(PORT, () => logger.info(`Server running on: ${ip.address():${PORT}`));
export default server;
And this is my index.test.js
import server from '../src/index.js';
import chai from 'chai';
import chaiHttp from 'chaihttp';
chai.should();
chai.use(chaiHttp);
describe("Testing RESTAPI", () => {
describe("Testing Basic Request", () => {
it ("Should return an ok message", (done) => {
chai.request(server)
.get('/')
.end((err, response) => {
response.should.have.status(200);
done();
});
});
});
});
And the message I got from console is like this:
1) Uncaught error outside test suite:
Uncaught Error: listen EADDRINUSE: address already in use :::3050
I would appreciate any response, thank you!