Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
express-mongo-testing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Harshith Karkera
express-mongo-testing
Commits
fb30216e
Commit
fb30216e
authored
3 months ago
by
Harshith Karkera
Browse files
Options
Downloads
Patches
Plain Diff
code cleanup
parent
216a6392
Branches
master
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
models/User.js
+0
-10
0 additions, 10 deletions
models/User.js
server.js
+0
-73
0 additions, 73 deletions
server.js
tests/user.test.js
+0
-86
0 additions, 86 deletions
tests/user.test.js
with
0 additions
and
169 deletions
models/User.js
+
0
−
10
View file @
fb30216e
// const mongoose = require("mongoose");
// const UserSchema = new mongoose.Schema({
// name: { type: String, required: true },
// email: { type: String, required: true, unique: true },
// age: { type: Number },
// });
// module.exports = mongoose.model("User", UserSchema);
const
mongoose
=
require
(
"
mongoose
"
);
const
UserSchema
=
new
mongoose
.
Schema
({
...
...
This diff is collapsed.
Click to expand it.
server.js
+
0
−
73
View file @
fb30216e
// const express = require("express");
// const mongoose = require("mongoose");
// const dotenv = require("dotenv");
// const morgan = require("morgan");
// // Load environment variables
// dotenv.config();
// const app = express();
// const PORT = process.env.PORT || 3000;
// // Middleware
// app.use(express.json()); // Parse JSON request body
// app.use(morgan("dev")); // Logging middleware
// // Connect to MongoDB Atlas
// mongoose
// .connect(process.env.MONGO_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// })
// .then(() => console.log("MongoDB Connected Successfully!"))
// .catch((err) => console.error("MongoDB Connection Error:", err));
// // User Schema & Model
// const userSchema = new mongoose.Schema({
// name: String,
// email: String,
// age: Number,
// });
// const User = mongoose.model("User", userSchema);
// // Routes
// // Create a new user (POST)
// app.post("/users", async (req, res) => {
// try {
// const newUser = new User(req.body);
// await newUser.save();
// res.status(201).json(newUser);
// } catch (err) {
// res.status(500).json({ error: err.message });
// }
// });
// // Get all users (GET)
// app.get("/users", async (req, res) => {
// try {
// const users = await User.find();
// res.json(users);
// } catch (err) {
// res.status(500).json({ error: err.message });
// }
// });
// // Update a user (PUT)
// app.put("/users/:id", async (req, res) => {
// try {
// const updatedUser = await User.findByIdAndUpdate(req.params.id, req.body, {
// new: true,
// });
// res.json(updatedUser);
// } catch (err) {
// res.status(500).json({ error: err.message });
// }
// });
// // Start the server
// app.listen(PORT, () => {
// console.log(`Server running on http://localhost:${PORT}`);
// });
const
express
=
require
(
"
express
"
);
const
mongoose
=
require
(
"
mongoose
"
);
const
dotenv
=
require
(
"
dotenv
"
);
...
...
This diff is collapsed.
Click to expand it.
tests/user.test.js
+
0
−
86
View file @
fb30216e
// const request = require("supertest");
// const app = require("../server"); // Import your Express server
// const mongoose = require("mongoose");
// // Sample user data
// const userData = { name: "Rahul", email: "Rahul@example.com", age: 25 };
// describe("User API Tests", () => {
// // Close the DB connection after tests
// afterAll(async () => {
// await mongoose.connection.close();
// });
// // Test: Create a new user
// it("should create a new user", async () => {
// const res = await request(app).post("/users").send(userData);
// expect(res.status).toBe(201);
// expect(res.body).toHaveProperty("_id");
// expect(res.body.name).toBe(userData.name);
// });
// // Test: Get all users
// it("should return a list of users", async () => {
// const res = await request(app).get("/users");
// expect(res.status).toBe(200);
// expect(res.body).toBeInstanceOf(Array);
// });
// // Test: Update a user
// it("should update a user", async () => {
// const newUser = await request(app).post("/users").send(userData);
// const userId = newUser.body._id;
// const updatedData = { name: "Rahul", age: 30 };
// const res = await request(app).put(`/users/${userId}`).send(updatedData);
// expect(res.status).toBe(200);
// expect(res.body.name).toBe(updatedData.name);
// });
// });
// const request = require("supertest");
// const mongoose = require("mongoose");
// const app = require("../server");
// require("dotenv").config(); // Load env variables
// const User = require("../models/User"); // Ensure path is correct
// describe("User API Tests", () => {
// beforeAll(async () => {
// await mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// });
// beforeEach(async () => {
// await User.deleteMany({}); // Clear database before each test
// });
// afterAll(async () => {
// await mongoose.connection.close();
// });
// it("should create a new user", async () => {
// const res = await request(app).post("/users").send({ name: "John Doe", email: "john@example.com", age: 25 });
// expect(res.status).toBe(201);
// expect(res.body).toHaveProperty("_id");
// });
// it("should return a list of users", async () => {
// await request(app).post("/users").send({ name: "Alice", email: "alice@example.com", age: 22 });
// const res = await request(app).get("/users");
// expect(res.status).toBe(200);
// expect(res.body.length).toBeGreaterThan(0);
// });
// it("should update a user", async () => {
// const newUser = await request(app).post("/users").send({ name: "John Doe", email: "john@example.com", age: 25 });
// const userId = newUser.body._id;
// const res = await request(app).put(`/users/${userId}`).send({ name: "Jane Doe", age: 30 });
// expect(res.status).toBe(200);
// expect(res.body.name).toBe("Jane Doe");
// });
// });
const
request
=
require
(
"
supertest
"
);
const
mongoose
=
require
(
"
mongoose
"
);
const
{
app
,
server
}
=
require
(
"
../server
"
);
// Import the server instance
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment