From 1bbe1d4a882f693a4895bcbb1ee1cd973d677dd6 Mon Sep 17 00:00:00 2001 From: gitea_admin Date: Fri, 6 Feb 2026 17:00:57 +0000 Subject: [PATCH] tests file --- tests/server.test.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/server.test.js diff --git a/tests/server.test.js b/tests/server.test.js new file mode 100644 index 0000000..03eedd1 --- /dev/null +++ b/tests/server.test.js @@ -0,0 +1,53 @@ +// Basic tests for backend API +const request = require('supertest'); +const app = require('../src/server'); + +describe('Backend API Tests', () => { + // Health check tests + describe('GET /health', () => { + it('should return 200 and status ok', async () => { + const res = await request(app).get('/health'); + expect(res.statusCode).toBe(200); + expect(res.body).toHaveProperty('status', 'ok'); + }); + }); + + // Readiness check tests + describe('GET /ready', () => { + it('should return readiness status', async () => { + const res = await request(app).get('/ready'); + expect([200, 503]).toContain(res.statusCode); + expect(res.body).toHaveProperty('status'); + expect(res.body).toHaveProperty('database'); + }); + }); + + // 404 handler test + describe('GET /nonexistent', () => { + it('should return 404 for unknown routes', async () => { + const res = await request(app).get('/nonexistent'); + expect(res.statusCode).toBe(404); + expect(res.body).toHaveProperty('error'); + }); + }); + + // Items API tests + describe('POST /api/items', () => { + it('should reject invalid item data', async () => { + const res = await request(app) + .post('/api/items') + .send({ name: '', quantity: -1, price: 'invalid' }); + + expect(res.statusCode).toBe(400); + expect(res.body).toHaveProperty('errors'); + }); + + it('should reject missing required fields', async () => { + const res = await request(app) + .post('/api/items') + .send({ quantity: 10 }); + + expect(res.statusCode).toBe(400); + }); + }); +}); \ No newline at end of file