Exceeded timeout of 5000 ms for a test JEST testing Express

Viewed 598

Im trying to write tests for heroku app, which uses postresql, but looks like my tests functions cannot connect to the database. The endpoints itself are working fine and I receive response from the server when I test it on the browser. Thanks in advance!! my code:
server.js

import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';

import mountRouter from './routes/index.js';

const port = 7000;
const app = express();


app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true}));
app.use(cors());

mountRouter(app);

app.get('/', (req, res) => {
    res.send("Welcome. The connection has been established.");
});

app.get('/test', (req, res) => {
    res.send("Test passed.");
});

app.listen(process.env.PORT || port, () => {
    console.log(`Server listening`)
  })

export default app;

index.js

import patientsRouter from './patients.js';
import staffMembersRouter from './staffMembers.js';
import hospitalWardsRouter from './hospitalWards.js';
import authenticationRouter from './authentication.js';

const mountRouter = app => {
    app.use('/patients', patientsRouter)
    app.use('/staff', staffMembersRouter)
    app.use('/wards', hospitalWardsRouter)
    app.use('/authentication', authenticationRouter)
}

export default mountRouter;

hospitalWardsController.js

import pool from '../db/db.js';
import sqlTemplate from 'sql-template-strings';

const { SQL } = sqlTemplate;

class HospitalWardsController {

    getAllHospitalWards = async (req, res) => {
        try {
            const allHospitalWards = await pool.query(SQL`
                SELECT * FROM hospital_ward;
                `);

            res.status(200).json(allHospitalWards.rows);
        } catch (err) {
            console.log(err.message);
        }
    }

    getFreeBedsCount = async (req, res) => {
        try {
            const { hospital_ward } = req.body;
            const freeBedsCount = await pool.query(SQL`
                SELECT hospital.name AS hospital_name, hospital.location AS hospital_location, hospital_ward.name AS hospital_ward_name, (bed_quantity - bed_reserved) AS beds_free
                FROM hospital_ward
                INNER JOIN hospital
                ON hospital_ward.hospital_id = hospital.id
                WHERE hospital_ward.name = ${hospital_ward}
                ORDER BY beds_free DESC;
            `);
            
            res.status(200).json(freeBedsCount.rows);
        } catch (err) {
            console.log(err.message);
        }
    }
}

const hospitalWardsController = new HospitalWardsController();

export default hospitalWardsController;

wards.test.js

import request from 'supertest';
import hospitalWardsController from '../routes/hospitalWards.js';
import app from '../server.js';




    describe("hospitalWards.js TESTS", () => {
    
        describe("hospitalWards.js GET('/') method TEST",  () => {           
   
            test("should return all hospital wards", async () => {
                app.listen();
                                
                const response = await request(hospitalWardsController).get("/").send({});
                expect(response.statusCode).toBe(200);
             });                               
        });
    });
    

0 Answers
Related