Skip to content

Commit d4925fc

Browse files
committed
Add e2e test for server
1 parent 03f6b05 commit d4925fc

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/server/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
NotFoundException,
55
} from "./src/index.js";
66

7-
const app = createAppServer({ port: 3003 });
7+
export const app = createAppServer({ port: 3003 });
88

99
// curl http://localhost:3003/hello?name=Stanley
1010
app.get(

packages/server/app.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)