Skip to content

Commit

Permalink
test: User Model test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsheel12 committed Sep 18, 2024
1 parent 9333bf0 commit bc785b9
Show file tree
Hide file tree
Showing 3 changed files with 820 additions and 5 deletions.
81 changes: 81 additions & 0 deletions backend/models/__tests__/User.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";
import User from "../User";
import { afterAll, beforeAll, beforeEach, expect, it } from "vitest";
import { describe } from "node:test";

let mongod: MongoMemoryServer;

const user1 = {
name: "Harsheel",
email: "hsin212",
password: "password",
};

const user2 = {
name: "Adi",
email: "ashe194",
password: "Password",
};

const user3 = {
name: "Adi",
email: "slie",
password: "Password",
};

const users = [user1, user2, user3];

/**
* Before all tests, create an in-memory MongoDB instance so we don't have to test on a real database,
* then establish a mongoose connection to it.
*/
beforeAll(async () => {
mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
await mongoose.connect(uri);
});

/**
* Before each test, intialize the database with some data
*/
beforeEach(async () => {
await mongoose.connection.db.dropDatabase();
const usersCollection = mongoose.connection.db.collection("users");
await usersCollection.insertMany(users);
});

/**
* After all tests, gracefully terminate the in-memory MongoDB instance and mongoose connection.
*/
afterAll(async () => {
await mongoose.disconnect();
await mongod.stop();
});

describe("User Model", () => {
it("find all the users", async () => {
const users = await User.find();
expect(users.length).toBe(3);
});

it("find a user by name", async () => {
const foundUser = await User.findOne({ name: "Harsheel" });
expect(foundUser?.name).toBe("Harsheel");
});

it("does not find user by invalid name", async () => {
const user = await User.findOne({ name: "invalid" });
expect(user).toBeNull();
});

it("find a user by email", async () => {
const foundUser = await User.findOne({ email: "hsin212" });
expect(foundUser?.email).toBe("hsin212");
});

it("does not find user by invalid email", async () => {
const user = await User.findOne({ email: "[email protected]" });
expect(user).toBeNull();
});
});
5 changes: 4 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build": "tsc",
"start": "node dist/index.js",
"dev": "nodemon index.ts",
"email": "email dev"
"email": "email dev",
"test": "vitest"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
Expand All @@ -33,8 +34,10 @@
"express": "^4.18.2",
"express-cors": "^0.0.3",
"jsonwebtoken": "^9.0.2",
"mongodb-memory-server": "^10.0.0",
"mongoose": "^8.1.1",
"ts-node": "^10.9.2",
"vitest": "^2.1.1",
"zod": "^3.22.4"
}
}
Loading

0 comments on commit bc785b9

Please sign in to comment.