diff --git a/__test__/controllers/jobs/getJob.controller.test.ts b/__test__/controllers/jobs/getJob.controller.test.ts new file mode 100644 index 0000000..c8b06ec --- /dev/null +++ b/__test__/controllers/jobs/getJob.controller.test.ts @@ -0,0 +1,110 @@ +/* eslint-disable @typescript-eslint/consistent-type-assertions */ +import { type Request, type Response } from "express"; +import { Job, type Queue } from "bullmq"; +import { queueByName } from "../../../src/queues"; +import getJob from "../../../src/controllers/jobs/getJob.controller"; + +// Mock the queueByName function +jest.mock("../../../src/queues", () => ({ + queueByName: jest.fn(), +})); + +// Mock the Job class +jest.mock("bullmq", () => ({ + Job: { + fromId: jest.fn(), + }, +})); + +describe("getJob", () => { + let req: Request; + let res: Response; + + beforeEach(() => { + req = { + params: {} + } as Request; + res = { + json: jest.fn(), + status: jest.fn().mockReturnThis(), + } as unknown as Response; + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should return job details if job exists", async () => { + const jobId = "job123"; + const queue = {} as Queue; + const job = { + id: jobId, + name: "Test Job", + getState: jest.fn().mockResolvedValue("completed"), + progress: 100, + data: { key: "value" }, + } as unknown as Job; + + // Mock the queueByName function to return the queue + (queueByName as jest.Mock).mockReturnValueOnce(queue); + + // Mock the Job.fromId function to return the job + (Job.fromId as jest.Mock).mockResolvedValueOnce(job); + + await getJob(req, res); + + expect(res.json).toHaveBeenCalledWith({ + id: job.id, + name: job.name, + status: "completed", + progress: job.progress, + data: job.data, + }); + expect(res.status).not.toHaveBeenCalled(); + }); + + it("should return 404 error if job does not exist", async () => { + const queue = {} as Queue; + + // Mock the queueByName function to return the queue + (queueByName as jest.Mock).mockReturnValueOnce(queue); + + // Mock the Job.fromId function to return null + (Job.fromId as jest.Mock).mockResolvedValueOnce(null); + + await getJob(req, res); + + expect(res.status).toHaveBeenCalledWith(404); + expect(res.json).toHaveBeenCalledWith({ error: "Job not found" }); + }); + + it("should handle error when queueByName throws an error", async () => { + const error = new Error("No Queue called unknownQueue"); + + // Mock the queueByName function to throw an error + (queueByName as jest.Mock).mockImplementationOnce(() => { + throw error; + }); + + await getJob(req, res); + + expect(res.status).toHaveBeenCalledWith(400); + expect(res.json).toHaveBeenCalledWith({ error }); + }); + + it("should handle error when Job.fromId throws an error", async () => { + const queue = {} as Queue; + const error = new Error("Failed to fetch job"); + + // Mock the queueByName function to return the queue + (queueByName as jest.Mock).mockReturnValueOnce(queue); + + // Mock the Job.fromId function to throw an error + (Job.fromId as jest.Mock).mockRejectedValueOnce(error); + + await getJob(req, res); + + expect(res.status).toHaveBeenCalledWith(500); + expect(res.json).toHaveBeenCalledWith({ error: "Failed to get job status" }); + }); +}); diff --git a/package.json b/package.json index ca7dcb9..0f006cd 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "npx tsc", "start": "node dist/index.js", "dev": "env-cmd nodemon ./src/index.ts", - "test": "env-cmd jest --coverage", + "test": "env-cmd jest --coverage --silent", "prettier": "npx prettier --write .", "lint": "npx eslint ." }, diff --git a/src/controllers/jobs/getJob.controller.ts b/src/controllers/jobs/getJob.controller.ts index 2d8755a..bbed605 100644 --- a/src/controllers/jobs/getJob.controller.ts +++ b/src/controllers/jobs/getJob.controller.ts @@ -11,7 +11,7 @@ const getJob = async function (req: Request, res: Response) { try { queue = queueByName(type) } catch (error) { - console.error(error) + console.error('Error getting job queue:', error) return res.status(400).json({ error }); } diff --git a/src/workers/emailWorker.ts b/src/workers/email.worker.ts similarity index 100% rename from src/workers/emailWorker.ts rename to src/workers/email.worker.ts diff --git a/src/workers/imageProcessingWorker.ts b/src/workers/imageProcessing.worker.ts similarity index 100% rename from src/workers/imageProcessingWorker.ts rename to src/workers/imageProcessing.worker.ts diff --git a/src/workers/index.ts b/src/workers/index.ts index 080d666..d614c9a 100644 --- a/src/workers/index.ts +++ b/src/workers/index.ts @@ -1,5 +1,5 @@ -import emailWorker from './emailWorker' -import imageProcessingWorker from './imageProcessingWorker' +import emailWorker from './email.worker' +import imageProcessingWorker from './imageProcessing.worker' export { emailWorker,