backend files

This commit is contained in:
2026-02-14 03:56:43 +00:00
commit 6876717edf
5 changed files with 201 additions and 0 deletions

35
backend/app.test.js Normal file
View File

@@ -0,0 +1,35 @@
const request = require('supertest');
const app = require('./index');
describe('Backend API', () => {
it('should return health check message on /', async () => {
const res = await request(app).get('/');
expect(res.statusCode).toEqual(200);
expect(res.text).toContain('Todo API is running');
});
it('should fetch all todos', async () => {
const res = await request(app).get('/api/todos');
expect(res.statusCode).toEqual(200);
expect(Array.isArray(res.body)).toBeTruthy();
expect(res.body.length).toBeGreaterThanOrEqual(2);
});
it('should add a new todo', async () => {
const newTodo = { task: 'Test task' };
const res = await request(app)
.post('/api/todos')
.send(newTodo);
expect(res.statusCode).toEqual(201);
expect(res.body.task).toEqual('Test task');
expect(res.body).toHaveProperty('id');
});
it('should fail to add a todo without a task', async () => {
const res = await request(app)
.post('/api/todos')
.send({});
expect(res.statusCode).toEqual(400);
expect(res.body.error).toEqual('Task is required');
});
});