diff --git a/backend/src/index.ts b/backend/src/index.ts index 641489a5..e9419127 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -8,6 +8,10 @@ const PORT = 5000 app.use(express.json()) app.use(cors()) +app.get("/health", (req, res) => { + res.json({ status: "ok"}); + }); + app.get("/api/notes", async (req, res) => { try { const notes = await prisma.note.findMany({ orderBy: { updatedAt: "desc" }}); diff --git a/backend/tests/service/api.spec.ts b/backend/tests/service/api.spec.ts index 754b11f5..20af52d3 100644 --- a/backend/tests/service/api.spec.ts +++ b/backend/tests/service/api.spec.ts @@ -4,11 +4,17 @@ import request from "supertest"; config(); -const URL = `${process.env.API_URL}/api/notes`; +const BASE_URL = `${process.env.API_URL}`; +const URL = `${BASE_URL}/api/notes`; let getNoteResponse: any; let createdID: number; +test("Health check", async () => { + const response = await request(BASE_URL).get('/health'); + expect(response.status).toBe(200); +}); + test("Get the list of Notes", async () => { getNoteResponse = await request(URL).get("/"); expect(getNoteResponse.status).toBe(200); diff --git a/backend/tests/unit/app.spec.ts b/backend/tests/unit/app.spec.ts index 5121d818..d9a8afeb 100644 --- a/backend/tests/unit/app.spec.ts +++ b/backend/tests/unit/app.spec.ts @@ -11,46 +11,64 @@ const seed = [ id: 1, title: "Meeting Notes", content: "Discussed project timelines and goals.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 2, title: "Shopping List", content: "Milk, eggs, bread, and fruits.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 3, title: "Recipe", content: "Ingredients: Chicken, tomatoes, onions, garlic.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 4, title: "Ideas", content: "Brainstorming ideas for the next feature release. 🚀", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 5, title: "Personal Goals", content: "Exercise for 30 minutes daily. Read a book every week.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 6, title: "Fête d'anniversaire", content: "Préparer une surprise pour la fête d'anniversaire.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 7, title: "日本旅行", content: "計画: 東京、京都、大阪を訪れる。", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 8, title: "Семейный ужин", content: "Приготовить вкусный ужин для всей семьи.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, { id: 9, title: "Coding Project", content: "Implement new features using React and Express.", + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:43:42.252Z") }, ]; @@ -70,6 +88,8 @@ describe("View notes", () => { id: 1, title: "Meeting Notes", content: "Discussed project timelines and goals.", + createdAt: "2024-02-05T23:43:42.252Z", + updatedAt: "2024-02-05T23:33:42.252Z", }, ]); }); @@ -78,7 +98,14 @@ describe("View notes", () => { prisma.note.findMany.mockResolvedValue(seed); const response = await request(app).get("/api/notes"); expect(response.status).toBe(200); - expect(response.body).toEqual(seed); + + const expectedResult = seed.map(item => ({ + ...item, + createdAt: new Date(item.createdAt).toISOString(), + updatedAt: new Date(item.updatedAt).toISOString() + })); + + expect(response.body).toEqual(expectedResult); }); test("500 error - failure", async ({}) => { prisma.note.findMany.mockImplementation(() => { @@ -95,6 +122,8 @@ describe("Create a note", () => { content: "Test", title: "Test", id: 1, + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:33:42.252Z"), }); const response = await request(app) .post("/api/notes") @@ -142,6 +171,8 @@ describe("Update a note", () => { title: "Test update", content: "Test", id: 1, + updatedAt: new Date("2024-02-05T23:33:42.252Z"), + createdAt: new Date("2024-02-05T23:33:42.252Z"), }); const response = await request(app) .put("/api/notes/1") @@ -150,7 +181,9 @@ describe("Update a note", () => { expect(response.body).toStrictEqual({ title: "Test update", content: "Test", + createdAt: "2024-02-05T23:33:42.252Z", id: 1, + updatedAt: "2024-02-05T23:33:42.252Z" }); }); test("PUT without title - failure", async ({}) => { @@ -224,3 +257,11 @@ describe("Delete a note", () => { expect(response.body).toStrictEqual({ error: "ID field required" }); }); }); + +describe("Health check", () => { + test("GET /health", async ({}) => { + const response = await request(app).get("/health"); + expect(response.status).toBe(200); + expect(response.body).toStrictEqual({ status: "ok" }); + }); +});