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

WORK-RRE
Rémi REGNAULT 1 year ago
parent 01c8cce238
commit c5866443c0

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

@ -24,6 +24,13 @@ app.use('/steps', StepsController);
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;

@ -1,13 +1,23 @@
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', () => {
it('should return a 200 status code', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
});
let server: Server<typeof IncomingMessage, typeof ServerResponse>;
beforeAll(() => {
server = startServer();
});
// Ecrire d'autres tests ici
afterAll((done) => {
server.close(done);
});
it('should return a 200 status code', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
});
// 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