mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Added tests for the event endpoints
Also installed chai, chai http & mocha
This commit is contained in:
Generated
+875
-2
File diff suppressed because it is too large
Load Diff
+8
-2
@@ -4,9 +4,9 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "ts-node src/app.ts",
|
||||
"dev": "nodemon src/app.ts"
|
||||
"dev": "nodemon src/app.ts",
|
||||
"test": "mocha -r ts-node/register --timeout 10000 src/Tests/*.ts"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -20,12 +20,18 @@
|
||||
"homepage": "https://github.com/detleph/server#readme",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^3.2.1",
|
||||
"@types/chai-http": "^4.2.0",
|
||||
"chai-http": "^4.3.0",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.2.22",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/mocha": "^9.0.0",
|
||||
"@types/node": "^16.10.3",
|
||||
"chai": "^4.3.4",
|
||||
"mocha": "^9.1.3",
|
||||
"prisma": "^3.2.1",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.4.3"
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
process.env.NODE_ENV = "test";
|
||||
|
||||
import chai from "chai";
|
||||
import chaiHttp from "chai-http";
|
||||
import server from "../app";
|
||||
import prisma from "../lib/prisma";
|
||||
|
||||
const should = chai.should();
|
||||
|
||||
chai.use(chaiHttp);
|
||||
|
||||
describe("events", () => {
|
||||
beforeEach("clear database", (done) => {
|
||||
prisma.event.deleteMany({}).then((a) => done());
|
||||
});
|
||||
|
||||
describe("Getall events", () => {
|
||||
it("should return an empty array", (done) => {
|
||||
chai
|
||||
.request(server)
|
||||
.get("/api/events/")
|
||||
.end((err, res) => {
|
||||
should.not.exist(err);
|
||||
res.should.have.status(204);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("should return an array with all the objects", (done) => {
|
||||
prisma.event
|
||||
.create({
|
||||
data: { date: new Date(Date.now()).toISOString(), name: "yes" },
|
||||
})
|
||||
.then((a) => {
|
||||
chai
|
||||
.request(server)
|
||||
.get("/api/events")
|
||||
.end((err, res) => {
|
||||
should.not.exist(err);
|
||||
res.should.have.status(200);
|
||||
res.body.should.be.a("array");
|
||||
res.body.length.should.be.eql(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("#add", () => {
|
||||
it("should return object", (done) => {
|
||||
let event = {
|
||||
date: new Date(Date.now()).toISOString(),
|
||||
name: "test",
|
||||
};
|
||||
|
||||
chai
|
||||
.request(server)
|
||||
.post("/api/events")
|
||||
.send(event)
|
||||
.end((err, res) => {
|
||||
should.not.exist(err);
|
||||
res.should.have.status(201);
|
||||
res.should.be.a("object");
|
||||
res.body.should.have.property("pid");
|
||||
res.body.should.have.property("name");
|
||||
res.body.should.have.property("date");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should return 400 if the request is malformed", (done) => {
|
||||
chai
|
||||
.request(server)
|
||||
.post("/api/events")
|
||||
.send()
|
||||
.end((err, res) => {
|
||||
should.not.exist(err);
|
||||
res.should.have.status(400);
|
||||
res.body.should.not.have.property("");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,7 @@ async function main() {
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
// All API endpoints are behind /api/...
|
||||
app.use("/api/events", eventRouter);
|
||||
|
||||
app.listen(process.env.PORT, () => {
|
||||
@@ -27,3 +28,5 @@ main()
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
Reference in New Issue
Block a user