-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from TogetherCrew/project-config
BullMQ
- Loading branch information
Showing
29 changed files
with
1,157 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* eslint-disable @typescript-eslint/consistent-type-assertions */ | ||
import { type Request, type Response } from "express"; | ||
import { type Queue } from "bullmq"; | ||
import { queueByName } from "../../../src/queues"; | ||
import createJob from "../../../src/controllers/jobs/createJob.controller"; | ||
|
||
console.error = jest.fn(); | ||
|
||
// Mock the queueByName function | ||
jest.mock("../../../src/queues", () => ({ | ||
queueByName: jest.fn(), | ||
})); | ||
|
||
// Mock the queue's add method | ||
const addMock = jest.fn(); | ||
|
||
// Create a mock queue object | ||
const queueMock = { | ||
add: addMock, | ||
} as unknown as Queue; | ||
|
||
describe("createJob", () => { | ||
let req: Request; | ||
let res: Response; | ||
|
||
beforeEach(() => { | ||
req = { | ||
body: { | ||
type: "email", | ||
data: { key: "value" }, | ||
}, | ||
} as Request; | ||
res = { | ||
json: jest.fn(), | ||
status: jest.fn().mockReturnThis(), | ||
} as unknown as Response; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should add the job to the appropriate queue and return the jobId", async () => { | ||
// Mock the queueByName function to return the queue | ||
(queueByName as jest.Mock).mockReturnValueOnce(queueMock); | ||
|
||
// Mock the queue's add method to return a job | ||
const jobId = "job123"; | ||
const job = { id: jobId }; | ||
addMock.mockResolvedValueOnce(job); | ||
|
||
await createJob(req, res); | ||
|
||
expect(queueByName).toHaveBeenCalledWith("email"); | ||
expect(addMock).toHaveBeenCalledWith("email", { key: "value" }); | ||
expect(res.json).toHaveBeenCalledWith({ jobId }); | ||
expect(res.status).not.toHaveBeenCalled(); | ||
}); | ||
|
||
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 createJob(req, res); | ||
|
||
expect(queueByName).toHaveBeenCalledWith("email"); | ||
expect(addMock).not.toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(400); | ||
expect(res.json).toHaveBeenCalledWith({ error }); | ||
expect(console.error).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle error when the queue's add method throws an error", async () => { | ||
const error = new Error("Failed to add job"); | ||
addMock.mockRejectedValueOnce(error); | ||
|
||
// Mock the queueByName function to return the queue | ||
(queueByName as jest.Mock).mockReturnValueOnce(queueMock); | ||
|
||
await createJob(req, res); | ||
|
||
expect(queueByName).toHaveBeenCalledWith("email"); | ||
expect(addMock).toHaveBeenCalledWith("email", { key: "value" }); | ||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalledWith({ error: "Failed to create job" }); | ||
expect(console.error).toHaveBeenCalledWith("Error creating job:", error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* 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", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { type Job } from "bullmq"; | ||
import emailJob from "../../src/jobs/email.job"; | ||
|
||
describe("emailJob", () => { | ||
let consoleLogSpy: jest.SpyInstance; | ||
let consoleErrorSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
consoleLogSpy = jest.spyOn(console, "log"); | ||
consoleErrorSpy = jest.spyOn(console, "error"); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it("should process email job successfully", async () => { | ||
const jobData = { | ||
to: "[email protected]", | ||
subject: "Test Subject", | ||
body: "Test Body", | ||
}; | ||
|
||
const job: Partial<Job> = { | ||
id: "jobId", | ||
data: jobData, | ||
name: "emailJob", | ||
}; | ||
|
||
await emailJob(job as Job); | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
`Sending email to ${jobData.to}: ${jobData.subject}` | ||
); | ||
expect(consoleLogSpy).toHaveBeenCalledWith("Body:", jobData.body); | ||
expect(consoleLogSpy).toHaveBeenCalledWith("Email sent successfully"); | ||
expect(consoleErrorSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { type Job } from "bullmq"; | ||
import imageProcessingJob from "../../src/jobs/imageProcessing.job"; | ||
|
||
describe("imageProcessingJob", () => { | ||
it("should process the image successfully", async () => { | ||
const imageUrl = "https://example.com/image.jpg"; | ||
const jobData = { imageUrl }; | ||
const job: Partial<Job> = { | ||
id: "job-id", | ||
name: "image-processing", | ||
data: jobData, | ||
// Mock other attributes as needed | ||
}; | ||
|
||
await expect(imageProcessingJob(job as Job)).resolves.toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import emailQueue from "../../src/queues/email.queue"; | ||
import imageProcessingQueue from "../../src/queues/imageProcessing.queue"; | ||
import queueByName from "../../src/queues/queueByName"; | ||
|
||
describe("queueByName", () => { | ||
afterAll(async () => { | ||
await emailQueue.close(); | ||
await imageProcessingQueue.close(); | ||
}); | ||
|
||
it("should return emailQueue for name 'email'", () => { | ||
expect(queueByName("email")).toBe(emailQueue); | ||
}); | ||
|
||
it("should return imageProcessingQueue for name 'imageProcessing'", () => { | ||
expect(queueByName("imageProcessing")).toBe(imageProcessingQueue); | ||
}); | ||
|
||
it("should throw an error for unknown queue name", () => { | ||
const unknownName = "unknownQueue"; | ||
const expectedErrorMessage = `No Queue called ${unknownName}`; | ||
expect(() => queueByName(unknownName)).toThrowError(expectedErrorMessage); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.