From d3982da2ce314fe196731c4e50e631e065b6946e Mon Sep 17 00:00:00 2001 From: Cyrille Derche Date: Sat, 20 May 2023 21:27:47 +0200 Subject: [PATCH] Add /posts route --- __test__/routes/posts.route.test.ts | 31 +++++++++++++++++++++++++++++ package.json | 2 +- src/app.ts | 3 +++ src/config/env.ts | 2 +- src/index.ts | 2 +- src/routes/posts.route.ts | 9 +++++++++ 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 __test__/routes/posts.route.test.ts create mode 100644 src/routes/posts.route.ts diff --git a/__test__/routes/posts.route.test.ts b/__test__/routes/posts.route.test.ts new file mode 100644 index 0000000..f5b3e81 --- /dev/null +++ b/__test__/routes/posts.route.test.ts @@ -0,0 +1,31 @@ +import supertest from "supertest"; +import app from "../../src/app"; +import mongoose from "mongoose"; +import Post from "../../src/models/post.model"; +import { env } from "../../src/config"; + +beforeEach(async () => { + mongoose.set("strictQuery", true); + await mongoose.connect(env.MONGODB_URL); +}); + +afterEach(async () => { + await mongoose.connection.db.dropDatabase(); + await mongoose.connection.close(); +}); + +describe("/posts routes", () => { + test("GET /posts", async () => { + const post = await Post.create({ + title: "Post 1", + content: "Lorem ipsum", + }); + + const res = await supertest(app).get("/posts").expect(200); + + expect(res.body.length).toEqual(1); + expect(res.body[0]._id).toBe(post.id); + expect(res.body[0].title).toBe(post.title); + expect(res.body[0].content).toBe(post.content); + }); +}); diff --git a/package.json b/package.json index 5eec450..a5ec5f7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "npx tsc", "start": "node dist/index.js", "dev": "env-cmd nodemon ./src/index.ts", - "test": "jest", + "test": "env-cmd jest", "prettier": "npx prettier --write .", "lint": "npx eslint ." }, diff --git a/src/app.ts b/src/app.ts index b01d161..60b007f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,10 @@ import express, { type Express, type Request, type Response } from "express"; +import postsRoute from "./routes/posts.route"; const app: Express = express(); +app.use("/posts", postsRoute); + app.get("/", (req: Request, res: Response) => { res.send("Express + TypeScript Server"); }); diff --git a/src/config/env.ts b/src/config/env.ts index 30ad8fd..31ff1e2 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ import Joi from "joi"; const schema = Joi.object() @@ -21,7 +22,6 @@ if (error != null) { throw new Error(`Config validation error: ${error.message}`); } -// eslint-disable-next-line @typescript-eslint/restrict-template-expressions const MONGODB_URL = `mongodb://${env.MONGODB_USER}:${env.MONGODB_PASS}@${ env.MONGODB_HOST }:${env.MONGODB_PORT}/${ diff --git a/src/index.ts b/src/index.ts index e03fe7f..baf84b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ import mongoose from "mongoose"; import app from "./app"; import { env } from "./config"; @@ -11,7 +12,6 @@ main().catch((err) => { async function main(): Promise { await mongoose.connect(env.MONGODB_URL); app.listen(env.PORT, () => { - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions console.log( `⚡️[server]: Server is running at http://localhost:${env.PORT}` ); diff --git a/src/routes/posts.route.ts b/src/routes/posts.route.ts new file mode 100644 index 0000000..f2ca707 --- /dev/null +++ b/src/routes/posts.route.ts @@ -0,0 +1,9 @@ +import express from "express"; +import { getPosts } from "../controllers/posts"; + +const router = express.Router(); + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +router.get("/", getPosts); + +export default router;