I am trying to test my API endpoints with jest and supertest:
my test routes file:
const app = require('../../index')
const request = require('supertest')
describe('test app endpoints', ()=>{
test('index should return 200 code', async (done) =>{
const response = await request(app).get('/')
expect(response.statusCode).toBe(200)
done()
})
index.js:
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/', require('./routes/conversions'))
module.exports = app.listen(port, (err) => {
if (err) throw err
console.log(`Server is running on port ${port}`)
})
When I run this test I get error about occupied port:
listen EADDRINUSE: address already in use :::3000
How can I fix that and do not block my port for tests?