From d749743e56dde9bf7232c7f2415d14a6c826311d Mon Sep 17 00:00:00 2001 From: Sugui Date: Mon, 25 Dec 2023 12:15:19 +0100 Subject: [PATCH] Added unit testing --- package.json | 13 ++++++++++--- src/index.ts | 2 +- tests/app.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 tests/app.test.ts diff --git a/package.json b/package.json index 2d7a2ae..66c9443 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,20 @@ "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest" + "@types/jest": "^29.5.11", + "@types/supertest": "^6.0.1", + "bun-types": "latest", + "jest": "^29.7.0", + "supertest": "^6.3.3", + "ts-jest": "^29.1.1" }, "peerDependencies": { "typescript": "^5.0.0" - },"scripts": { + }, + "scripts": { "start": "bun run src/index.ts", - "dev": "bun --hot run src/index.ts" + "dev": "bun --hot run src/index.ts", + "test": "bun test" }, "dependencies": { "@types/express": "^4.17.21", diff --git a/src/index.ts b/src/index.ts index 10401fc..bde001d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import express from "express"; import mongoose from "mongoose"; import ImageModel from "./ImageModel"; -const app = express(); +export const app = express(); app.use(express.json()); diff --git a/tests/app.test.ts b/tests/app.test.ts new file mode 100644 index 0000000..feb519f --- /dev/null +++ b/tests/app.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "bun:test"; +import request from "supertest"; +import { app } from "../src"; + +describe("GET /images works properly", () => { + it("should be an array", async () => { + const res = await request(app).get("/images"); + expect(Array.isArray(res.body)).toBeTrue(); + expect(res.statusCode).toBe(200); + }); +}); + +describe("POST /images works properly", () => { + it("should return 201 or 409", async () => { + const res = await request(app).post("/images").send({ + url: "https://test.url.com/123", + status: "available", + tags: ["2girls", "touhou"] + }); + expect(res.status).toSatisfy(status => [201, 409].includes(status)); + }); +}); \ No newline at end of file