Files
simple-node/backend/app.test.js
2026-02-14 03:56:43 +00:00

36 lines
1.1 KiB
JavaScript

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');
});
});