Skip to content

Commit

Permalink
Merge pull request #2 from TogetherCrew/project-config
Browse files Browse the repository at this point in the history
Mongoose
  • Loading branch information
cyri113 authored May 20, 2023
2 parents d0f5157 + c508caf commit 98c21da
Show file tree
Hide file tree
Showing 18 changed files with 649 additions and 61 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ docker compose -f docker-compose.test.yml up --exit-code-from app
```

Note: This will create a /coverage folder where you can review the coverage details.

### Development Environment

You can run all the integration services using the following command:

```bash
docker compose -f docker-compose.dev.yml up
```

#### Supported Services

- MongoDB
- Redis
41 changes: 41 additions & 0 deletions __test__/config/env.test.ts
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");
});
});
});
53 changes: 53 additions & 0 deletions __test__/controllers/posts/getPosts.controller.test.ts
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();
});
});
});
24 changes: 24 additions & 0 deletions __test__/models/post.model.test.ts
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();
});
});
});
31 changes: 31 additions & 0 deletions __test__/routes/posts.route.test.ts
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);
});
});
14 changes: 14 additions & 0 deletions docker-compose.dev.yml
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
4 changes: 0 additions & 4 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ services:
- mongo
redis:
image: "redis:alpine"
ports:
- 6379:6379
mongo:
image: "mongo"
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
Loading

0 comments on commit 98c21da

Please sign in to comment.