Skip to content

Commit 677fc47

Browse files
committed
added a negative scebario for testing
1 parent 9f4eb60 commit 677fc47

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

__test__/pet.test.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ const request = require("supertest");
33

44
describe("Pet Service", () => {
55
describe("Normal scenario", () => {
6-
it("should save the pet in the database", async () => {
7-
const response = await request(app).post("/api/pet").send({
8-
id: 5,
9-
name: "Sandy",
10-
type: "Hamster"
11-
});
12-
13-
expect(response.headers["content-type"]).toEqual(expect.stringContaining("application/json"));
14-
});
15-
166
it("should response with 201 when new pet created with all the fields", async () => {
177
const response = await request(app).post("/api/pet").send({
188
id: 5,
@@ -33,4 +23,27 @@ describe("Pet Service", () => {
3323
expect(response.headers["content-type"]).toEqual(expect.stringContaining("application/json"));
3424
});
3525
});
26+
27+
describe("Negative scenario", () => {
28+
it("should response with 400 when name ro type is missing", async () => {
29+
const bodyData = [
30+
{
31+
id: 5,
32+
name: "Sandy"
33+
},
34+
{
35+
id: 5,
36+
type: "Hamster"
37+
},
38+
{
39+
id: 5
40+
}
41+
];
42+
43+
for (const data of bodyData) {
44+
const response = await request(app).post("/api/pet").send(data);
45+
expect(response.statusCode).toBe(400);
46+
}
47+
});
48+
});
3649
});

app.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,4 @@ app.use(express.json());
66

77
app.use("/api", routes);
88

9-
app.listen("8080", () => {
10-
console.log("Server is up and running");
11-
});
12-
139
module.exports = app;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "An introduction of testing nodejs API using jest and supertest",
55
"main": "app.js",
66
"scripts": {
7-
"start": "nodemon app.js",
8-
"test": "jest"
7+
"start": "nodemon server.js",
8+
"test": "jest --watch"
99
},
1010
"keywords": [
1111
"nodejs",

routes/pet.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ const pets = require("../data");
33

44
router.post("/", async (req, res, next) => {
55
const body = req.body;
6+
const { name, type } = body;
7+
if (!name || !type) {
8+
res.status(400).json("Bad Request");
9+
return;
10+
}
11+
612
pets.push(body);
7-
res.status(200).json("Added successfully");
13+
res.status(201).json("Added successfully");
814
});
915

1016
module.exports = router;

server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const app = require("./app");
2+
3+
app.listen("8080", () => {
4+
console.log("Server is up and running");
5+
});

0 commit comments

Comments
 (0)