added a test for /

This commit is contained in:
Alie 2023-12-27 16:52:15 +01:00
parent f483f62c42
commit c5e41d6f62
2 changed files with 51 additions and 42 deletions

View File

@ -10,14 +10,14 @@ app.get("/", (_, res) => {
res.json({ message: "Blazing fast 🚀" }); res.json({ message: "Blazing fast 🚀" });
}); });
app.get("/images", async (req, res) => { app.get("/images", async (_, res) => {
try { try {
const allImages = await ImageModel.find(); const allImages = await ImageModel.find();
res.json(allImages); res.json({ message: allImages });
} catch (error) { } catch (error) {
res.status(500).json({ message: error }); res.status(500).json({ message: error });
} }
}) });
app.post("/images", async (req, res) => { app.post("/images", async (req, res) => {
try { try {
@ -25,7 +25,7 @@ app.post("/images", async (req, res) => {
// Thowing a 401 if not auth provided // Thowing a 401 if not auth provided
// Throwing a 403 for incorrect auth // Throwing a 403 for incorrect auth
const image = await ImageModel.create(req.body); const image = await ImageModel.create(req.body);
res.status(201).json(image); res.status(201).json({ message: image });
} catch (error: any) { } catch (error: any) {
if (error.code == 11000) { if (error.code == 11000) {
// Should return 409 Conflict for existing urls // Should return 409 Conflict for existing urls
@ -34,7 +34,7 @@ app.post("/images", async (req, res) => {
// Should return 400 Bad request for invalid requests // Should return 400 Bad request for invalid requests
res.status(400).json({ message: error }); res.status(400).json({ message: error });
} }
}) });
// Set the default port to 8080, or use the PORT environment variable // Set the default port to 8080, or use the PORT environment variable
@ -48,14 +48,15 @@ const start = async () => {
await mongoose.connect(mongo_uri, { await mongoose.connect(mongo_uri, {
authSource: "admin", authSource: "admin",
user: mongo_user, user: mongo_user,
pass: mongo_pass pass: mongo_pass,
}); });
app.listen(port, () => console.log(`Express server listening on port ${port}`)); app.listen(port, () =>
console.log(`Express server listening on port ${port}`)
);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
process.exit(1); process.exit(1);
} }
} };
start(); start();

View File

@ -1,12 +1,20 @@
import { beforeAll, describe, expect, it } from "bun:test"; import { describe, expect, it, mock } from "bun:test";
import request from "supertest"; import request from "supertest";
import { app } from "../src"; import { app } from "../src";
describe("GET / shows what it should",async () => {
const res = await request(app).get("/");
it("should be", async () => {
expect(res.body).toHaveProperty("message", "Blazing fast 🚀");
});
})
describe("GET /images works properly", async () => { describe("GET /images works properly", async () => {
const res = await request(app).get("/images"); const res = await request(app).get("/images");
it("should be an array", async () => { it("should be an array", async () => {
expect(Array.isArray(res.body)).toBeTrue(); expect(Array.isArray(res.body.message)).toBeTrue();
}); });
it("should return a 200", async () => { it("should return a 200", async () => {