Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"dotenv": "^16.4.7",
"express": "^4.21.2",
"jest": "^29.7.0",
"pg": "^8.16.3",
"pg-hstore": "^2.3.4",
"prettier": "3.4.2",
"sequelize": "^6.37.7",
"supertest": "^7.0.0",
"vitepress": "^1.5.0"
},
Expand Down
3 changes: 3 additions & 0 deletions templates/express_pg_sequelize/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
coverage
12 changes: 8 additions & 4 deletions templates/express_pg_sequelize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.get("/db-test", async (req, res) => {
const [results, metadata] = await db.query("SELECT NOW()");
return res
.status(200)
.json({ message: "DB query successful!", data: results });
.json({ message: "DB Query Successful!", data: results });
} catch (error) {
return res
.status(500)
Expand All @@ -36,6 +36,10 @@ app.get("/db-test", async (req, res) => {
});

// Start the server
app.listen(appConfig.PORT, () => {
console.log(`Server is running on http://localhost:${appConfig.PORT}`);
});
if (process.env.NODE_ENV !== "test") {
app.listen(appConfig.PORT, () => {
console.log(`Server is running on http://localhost:${appConfig.PORT}`);
});
}

export default app;
36 changes: 36 additions & 0 deletions templates/express_pg_sequelize/index.test.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to be same as the original js file - index.test.js - it'll improve readability

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes @Ashrockzzz2003

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { jest } from "@jest/globals";
import request from "supertest";

jest.unstable_mockModule("./db.js", () => ({
default: {
authenticate: jest.fn(() => Promise.resolve()),
query: jest.fn(() => Promise.resolve([[{ "now()": new Date() }], {}])),
},
}));

const { default: app } = await import("./index.js");
const { default: db } = await import("./db.js");

describe("express_pg_sequelize API Endpoints", () => {
it("should return welcome message on GET /", async () => {
const res = await request(app).get("/");
expect(res.statusCode).toEqual(200);
expect(res.text).toBe("Hello, Express with PostgreSQL!");
});

it("should return DB query results on GET /db-test", async () => {
const res = await request(app).get("/db-test");
expect(res.statusCode).toEqual(200);
expect(res.body.message).toBe("DB Query Successful!");
expect(res.body.data).toBeDefined();
});

it("should handle DB errors on GET /db-test", async () => {
db.query.mockImplementationOnce(() =>
Promise.reject(new Error("DB Error")),
);
const res = await request(app).get("/db-test");
expect(res.statusCode).toEqual(500);
expect(res.body.message).toBe("Error querying the database");
});
});
8 changes: 6 additions & 2 deletions templates/express_pg_sequelize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
"dev": "nodemon index.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"type": "module",
"dependencies": {
"express": "^4.17.1",
"pg": "^8.6.0",
Expand All @@ -14,6 +16,8 @@
"dotenv": "^16.4.7"
},
"devDependencies": {
"nodemon": "^3.1.9"
"nodemon": "^3.1.9",
"jest": "^29.7.0",
"supertest": "^7.0.0"
}
}