|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +import request from "supertest"; |
| 5 | +import { app } from "./app.js"; |
| 6 | + |
| 7 | +describe("/app", () => { |
| 8 | + it("GET /hello", async () => { |
| 9 | + // Happy path 200 use case |
| 10 | + const response = await request(app) |
| 11 | + .get("/hello") |
| 12 | + .set("Accept", "application/json"); |
| 13 | + |
| 14 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 15 | + expect(response.status).toEqual(200); |
| 16 | + expect(response.body).toEqual({ hello: "Stan" }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("GET /hello/Stan", async () => { |
| 20 | + // This endpoint explicitly 404s |
| 21 | + const response = await request(app) |
| 22 | + .get("/hello/Stan") |
| 23 | + .set("Accept", "application/json"); |
| 24 | + |
| 25 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 26 | + expect(response.status).toEqual(404); |
| 27 | + expect(response.body).toEqual({ error: "Not Found" }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("GET /hello/Sara", async () => { |
| 31 | + // Custom parameter 200 |
| 32 | + const response = await request(app) |
| 33 | + .get("/hello/Sara") |
| 34 | + .set("Accept", "application/json"); |
| 35 | + |
| 36 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 37 | + expect(response.status).toEqual(200); |
| 38 | + expect(response.body).toEqual({ hello: "Sara" }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("POST /hello", async () => { |
| 42 | + // POST request with JSON body 200 |
| 43 | + const response = await request(app) |
| 44 | + .post("/hello") |
| 45 | + .set("Accept", "application/json") |
| 46 | + .send({ name: "Henry" }); |
| 47 | + |
| 48 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 49 | + expect(response.status).toEqual(200); |
| 50 | + expect(response.body).toEqual({ hello: "Henry" }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments