unit testing #7

Merged
Suguivy merged 67 commits from testing into develop 2023-12-31 11:11:23 +00:00
9 changed files with 208 additions and 209 deletions
Showing only changes of commit 9cd61c101c - Show all commits

View File

@ -4,12 +4,10 @@
"type": "module",
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/mongodb-memory-server": "^2.3.0",
"@types/supertest": "^6.0.1",
"@types/express": "^4.17.21",
"@types/express-list-endpoints": "^6.0.3",
"@types/jsonwebtoken": "^9.0.5",
"@types/mongoose": "^5.11.97",
"bun-types": "latest",
"jest": "^29.7.0",
"mongodb-memory-server": "^9.1.3",
@ -30,10 +28,5 @@
"express-list-endpoints": "^6.0.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.0.3"
},
"config": {
"mongodbMemoryServer": {
"debug": "1"
}
}
}

View File

@ -36,6 +36,6 @@ export const startApp = async () => {
console.error(error);
process.exit(1);
}
};
};
export default app;

View File

@ -23,9 +23,7 @@ class ImageController {
} catch (error: any) {
if (error instanceof mongo.MongoServerError && error.code === 11000) {
// Should return 409 Conflict for existing urls
res
.status(409)
.json({
res.status(409).json({
error: `the image with URL ${error.keyValue.url} already exists`,
});
} else if (error instanceof mongoose.Error.ValidationError) {

View File

@ -3,10 +3,6 @@ import { startApp } from "./app";
await startApp();
try{
try {
await populateDatabase();
}
catch {
}
} catch {}

View File

@ -5,7 +5,8 @@ export interface Auth extends Document {
secret: String;
}
const AuthSchema = new mongoose.Schema({
const AuthSchema = new mongoose.Schema(
{
app: {
type: String,
required: true,
@ -15,6 +16,8 @@ const AuthSchema = new mongoose.Schema({
type: String,
required: true,
},
}, { collection: "authorizations" });
},
{ collection: "authorizations" }
);
export default mongoose.model("authorizations", AuthSchema);

View File

@ -6,12 +6,13 @@ export interface Image extends Document {
tags?: String[];
}
const ImageSchema = new mongoose.Schema({
const ImageSchema = new mongoose.Schema(
{
url: {
type: String,
required: true,
index: true,
unique: true
unique: true,
},
status: {
type: String,
@ -24,6 +25,8 @@ const ImageSchema = new mongoose.Schema({
tags: {
type: [String],
},
}, { collection: "images" });
},
{ collection: "images" }
);
export default mongoose.model<Image>("images", ImageSchema);

View File

@ -1,4 +1,12 @@
import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
mock,
} from "bun:test";
import request, { Response } from "supertest";
import app, { startApp } from "../src/app";
import imageService from "../src/services/ImageService";
@ -10,8 +18,7 @@ const imageServiceOriginal = imageService;
let token: string;
beforeAll(async () => {
if (!process.env.DEDICATED_MONGODB_SERVER)
await memoryServer.start();
//if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start();
await startApp();
await populateDatabase();
@ -21,11 +28,10 @@ beforeAll(async () => {
token = tok.body.token;
});
afterAll(async () => {
if (!process.env.DEDICATED_MONGODB_SERVER)
await memoryServer.stop();
})
/* afterAll(async () => {
if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop();
});
*/
afterEach(() => {
mock.restore();
mock.module("../src/services/ImageService", () => ({
@ -53,7 +59,7 @@ describe("/login works as instended", async () => {
const res = await request(app).post("/login").send({});
expect(res.status).toBe(403);
});
});
});
describe("GET / shows all of the endpoints", async () => {
const res = await request(app).get("/");