diff --git a/src/__tests__/integration.test.ts b/src/__tests__/integration.test.ts index a4ba974..63d045d 100644 --- a/src/__tests__/integration.test.ts +++ b/src/__tests__/integration.test.ts @@ -1,5 +1,18 @@ +import dotenv from 'dotenv'; +import path from 'path'; import request from 'supertest'; -import { app } from '../boot/setup'; +import type { Application } from 'express'; +import { createApp } from '../boot/setup'; + +let app: Application; + +beforeAll(async () => { + app = await createApp(); +}); + +dotenv.config({ path: path.resolve(__dirname, '../../.env.test') }); + +// const app = createApp(); describe('Integration Tests', () => { it('GET /health should return 200 OK', async () => { @@ -8,9 +21,9 @@ describe('Integration Tests', () => { expect(response.body).toHaveProperty('status', 'ok'); }); - it('GET /users (unauthenticated) should return 200 OK', async () => { - const response = await request(app).get('/users'); - expect(response.status).toBe(200); + it('POST /users/register with missing data should return 400', async () => { + const res = await request(app).post('/users/register').send({}); // Empty body to simulate missing fields + expect(res.status).toBe(400); // Adjust if your validation returns a different code }); it('GET /messages without token should return 401', async () => { @@ -18,7 +31,7 @@ describe('Integration Tests', () => { expect(response.status).toBe(401); }); - it('GET /unknown-route should return 404', async () => { + it('GET /nonexistent should return 404', async () => { const response = await request(app).get('/nonexistent'); expect(response.status).toBe(404); }); diff --git a/src/__tests__/setup.ts b/src/__tests__/setup.ts index b9c88d8..dcf64e0 100644 --- a/src/__tests__/setup.ts +++ b/src/__tests__/setup.ts @@ -17,8 +17,14 @@ jest.mock('pg', () => { connect: jest.fn(), query: jest.fn(), end: jest.fn(), + on: jest.fn(), + }; + return { + Pool: jest.fn(() => mockPool), + types: { + setTypeParser: jest.fn(), + }, }; - return { Pool: jest.fn(() => mockPool) }; }); // Mock winston logger diff --git a/src/boot/setup.ts b/src/boot/setup.ts index 9b1c260..50546f2 100644 --- a/src/boot/setup.ts +++ b/src/boot/setup.ts @@ -25,16 +25,23 @@ const PORT = process.env.PORT || 8080; const MONGO_URI = process.env.MONGO_URI; const SESSION_SECRET = process.env.SESSION_SECRET || 'default_secret'; -const app: Application = express(); const ENV = process.env.NODE_ENV || 'dev'; dotenv.config({ path: path.resolve(__dirname, `../../.env.${ENV}`) }); -try { - mongoose.connect(MONGO_URI as string); - logger.info(`MongoDB Connected to ${MONGO_URI}`); -} catch (error) { - logger.error('Error connecting to DB: ' + error); -} +const app: Application = express(); + +const connectMongo = async () => { + if (MONGO_URI && process.env.NODE_ENV !== 'test') { + try { + await mongoose.connect(MONGO_URI); + logger.info(`MongoDB Connected to ${MONGO_URI}`); + } catch (error) { + logger.error('Error connecting to DB: ' + error); + } + } else { + logger.warn('Skipping MongoDB connection (likely running in test mode)'); + } +}; // MIDDLEWARE const registerCoreMiddleWare = (): void => { @@ -73,10 +80,10 @@ const registerCoreMiddleWare = (): void => { // 404 handling for not found app.use(notFound); - logger.http('Done registering all middlewares'); + logger.info('Done registering all middlewares'); } catch (_err) { logger.error('Error thrown while executing registerCoreMiddleWare'); - process.exit(1); + throw _err; } }; @@ -89,9 +96,17 @@ const handleError = (): void => { }); }; +const createApp = async (): Promise => { + await connectMongo(); // only connects if not in test + registerCoreMiddleWare(); // apply middleware + routes + return app; +}; + // start applicatoin -const startApp = (): void => { +const startApp = async (): Promise => { try { + await connectMongo(); + // register core application level middleware registerCoreMiddleWare(); @@ -113,4 +128,4 @@ const startApp = (): void => { } }; -export { startApp, app }; +export { startApp, createApp }; diff --git a/src/routes/users.routes.ts b/src/routes/users.routes.ts index 885ef9c..66536d7 100644 --- a/src/routes/users.routes.ts +++ b/src/routes/users.routes.ts @@ -6,4 +6,4 @@ const router = Router(); router.post('/register', register); router.post('/login', login); -export default router; \ No newline at end of file +export default router;