2024-04-17 18:04:18 +00:00
|
|
|
import { afterEach, describe, expect, it, mock } from "bun:test";
|
2024-04-17 17:23:39 +00:00
|
|
|
import app from "src/app";
|
2024-04-17 18:04:18 +00:00
|
|
|
import ImageService from "src/services/ImageService";
|
2024-04-15 08:54:01 +00:00
|
|
|
import request from "supertest";
|
|
|
|
|
|
|
|
const imageServiceOriginal = ImageService;
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/ImageService", () => ({
|
2024-04-15 08:54:01 +00:00
|
|
|
default: imageServiceOriginal,
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("endpoint returns the correct status codes", () => {
|
|
|
|
it("should return 200 if successful", async () => {
|
|
|
|
// Can't mock ImageService partially because of bun incomplete implementation of testing :(
|
|
|
|
// It goes to the network directly to test
|
|
|
|
const res = await request(app).get("/image");
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 500 if any error happens", async () => {
|
2024-04-17 17:23:39 +00:00
|
|
|
mock.module("src/services/ImageService", () => {
|
2024-04-15 08:54:01 +00:00
|
|
|
return {
|
|
|
|
default: {
|
|
|
|
get: () => {
|
|
|
|
throw new Error("Controlled error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const res = await request(app).get("/image");
|
|
|
|
expect(res.status).toBe(500);
|
|
|
|
});
|
|
|
|
})
|