-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from TogetherCrew/project-config
Mongoose
- Loading branch information
Showing
18 changed files
with
649 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
describe("env", () => { | ||
describe("schema", () => { | ||
it("should validate the schema successfully", () => { | ||
expect.assertions(1); | ||
expect(() => { | ||
import("../../src/config/env"); | ||
}).not.toThrowError(); | ||
}); | ||
}); | ||
|
||
describe("environment variables", () => { | ||
const DEFAULT_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...DEFAULT_ENV }; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = DEFAULT_ENV; | ||
}); | ||
|
||
it("should throw an error if required environment variables are missing", () => { | ||
expect.assertions(1); | ||
delete process.env.MONGODB_HOST; | ||
const expectedErrorMessage = | ||
'Config validation error: "MONGODB_HOST" is required'; | ||
expect(() => { | ||
require("../../src/config/env"); | ||
}).toThrowError(expectedErrorMessage); | ||
}); | ||
|
||
it("should configure MONGODB_URL", () => { | ||
expect.assertions(1); | ||
process.env.MONGODB_NAME = "db"; | ||
const { env } = require("../../src/config/env"); | ||
expect(env.MONGODB_URL).toContain(":27017/db"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { type Request, type Response } from "express"; | ||
import Post from "../../../src/models/post.model"; | ||
import { getPosts } from "../../../src/controllers/posts"; | ||
|
||
// Mocking the Post model | ||
jest.mock("../../../src/models/post.model", () => ({ | ||
find: jest.fn().mockReturnThis(), | ||
exec: jest.fn(), | ||
})); | ||
|
||
describe("post controller", () => { | ||
describe("getPosts", () => { | ||
let req: Request; | ||
let res: Response; | ||
|
||
beforeEach(() => { | ||
res = { | ||
send: jest.fn(), | ||
sendStatus: jest.fn(), | ||
} as unknown as Response; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should retrieve and send posts", async () => { | ||
const posts = [{ title: "Post 1" }, { title: "Post 2" }]; | ||
|
||
// Mocking the Post.find().exec() method to return posts | ||
(Post.find().exec as jest.Mock).mockResolvedValue(posts); | ||
|
||
await getPosts(req, res); | ||
|
||
expect(res.send).toHaveBeenCalledWith(posts); | ||
expect(res.sendStatus).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("should send 500 status on error", async () => { | ||
const errorMessage = "Internal server error"; | ||
|
||
// Mocking the Post.find().exec() method to throw an error | ||
(Post.find().exec as jest.Mock).mockRejectedValue( | ||
new Error(errorMessage) | ||
); | ||
|
||
await getPosts(req, res); | ||
|
||
expect(res.sendStatus).toHaveBeenCalledWith(500); | ||
expect(res.send).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Post from "../../src/models/post.model"; | ||
|
||
describe("Post model", () => { | ||
describe("validation", () => { | ||
test("should validate a post", async function () { | ||
await expect( | ||
new Post({ | ||
title: "Lorem ipsum", | ||
content: "Lorem ipsum", | ||
}).validate() | ||
).resolves.toBeUndefined(); | ||
}); | ||
test("should have a title", async () => { | ||
await expect( | ||
new Post({ title: undefined, content: "" }).validate() | ||
).rejects.toThrow(); | ||
}); | ||
test("should have content", async () => { | ||
await expect( | ||
new Post({ title: "", content: undefined }).validate() | ||
).rejects.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: "3.9" | ||
|
||
services: | ||
redis: | ||
image: "redis:alpine" | ||
ports: | ||
- 6379:6379 | ||
mongo: | ||
image: "mongo" | ||
ports: | ||
- 27017:27017 | ||
environment: | ||
- MONGO_INITDB_ROOT_USERNAME=root | ||
- MONGO_INITDB_ROOT_PASSWORD=pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.