Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -8,17 +21,17 @@ 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 () => {
const response = await request(app).get('/messages');
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);
});
Expand Down
8 changes: 7 additions & 1 deletion src/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 26 additions & 11 deletions src/boot/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@
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 () => {

Check warning on line 33 in src/boot/setup.ts

View workflow job for this annotation

GitHub Actions / test-and-lint (18.x)

Missing return type on function
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 => {
Expand Down Expand Up @@ -73,10 +80,10 @@
// 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;
}
};

Expand All @@ -89,9 +96,17 @@
});
};

const createApp = async (): Promise<Application> => {
await connectMongo(); // only connects if not in test
registerCoreMiddleWare(); // apply middleware + routes
return app;
};

// start applicatoin
const startApp = (): void => {
const startApp = async (): Promise<void> => {
try {
await connectMongo();

// register core application level middleware
registerCoreMiddleWare();

Expand All @@ -113,4 +128,4 @@
}
};

export { startApp, app };
export { startApp, createApp };
2 changes: 1 addition & 1 deletion src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const router = Router();
router.post('/register', register);
router.post('/login', login);

export default router;
export default router;