tests: add tests for IngredientsController for route ingredients/ and ingredients/:id

WORK-RRE
Rémi REGNAULT 2 years ago
parent 01c8cce238
commit c5866443c0

@ -54,7 +54,8 @@ IngredientsController.get('/:id', async (req, res) => {
const id = Number(req.params.id); const id = Number(req.params.id);
if (!Number.isInteger(id)) { if (!Number.isInteger(id)) {
throw new Exceptions.BadRequestException('id invalid !'); res.status(400).send('invalid parameter')
return
} }
try { try {

@ -24,6 +24,13 @@ app.use('/steps', StepsController);
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
export const server = app.listen(port, () => console.log(`App listening on PORT ${port}`)); export const startServer = () => {
return app.listen(port, () => console.log(`App listening on PORT ${port}`));
};
// Exécutez le serveur uniquement si le module est le point d'entrée principal
if (require.main === module) {
startServer();
}
export default app; export default app;

@ -1,7 +1,18 @@
import request from 'supertest'; import request from 'supertest';
import app, {server} from '../src/server'; import app, { startServer } from '../src/server';
import { Server, IncomingMessage, ServerResponse } from 'http';
describe('GET /api/endpoint', () => { describe('GET /api/endpoint', () => {
let server: Server<typeof IncomingMessage, typeof ServerResponse>;
beforeAll(() => {
server = startServer();
});
afterAll((done) => {
server.close(done);
});
it('should return a 200 status code', async () => { it('should return a 200 status code', async () => {
const response = await request(app).get('/'); const response = await request(app).get('/');
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -9,5 +20,4 @@ describe('GET /api/endpoint', () => {
// Ecrire d'autres tests ici // Ecrire d'autres tests ici
server.close()
}); });

@ -0,0 +1,36 @@
import request from 'supertest';
import app, { startServer } from '../src/server';
import { Server, IncomingMessage, ServerResponse } from 'http';
describe('GET /ingredients', () => {
let server: Server<typeof IncomingMessage, typeof ServerResponse>;
beforeAll(() => {
server = startServer();
});
afterAll((done) => {
server.close(done);
});
it('should return a 200 status code', async () => {
const response = await request(app).get('/ingredients/');
expect(response.status).toBe(200);
});
it('should return a 200 status code', async () => {
const response = await request(app).get('/ingredients/1');
expect(response.status).toBe(200);
});
it('should return a 400 status code', async () => {
const response = await request(app).get('/ingredients/l');
expect(response.status).toBe(400);
});
it('should return a 404 status code', async () => {
const response = await request(app).get('/ingredients/8024');
expect(response.status).toBe(404);
});
});
Loading…
Cancel
Save