diff --git a/__test__/jobs/email.job.test.ts b/__test__/jobs/email.job.test.ts new file mode 100644 index 0000000..d9df9bf --- /dev/null +++ b/__test__/jobs/email.job.test.ts @@ -0,0 +1,39 @@ +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: 'example@example.com', + subject: 'Test Subject', + body: 'Test Body', + }; + + const job: Partial = { + 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(); + }); + +}); diff --git a/__test__/workers/email.worker.test.ts b/__test__/workers/email.worker.test.ts new file mode 100644 index 0000000..85ad882 --- /dev/null +++ b/__test__/workers/email.worker.test.ts @@ -0,0 +1,28 @@ +import { Worker } from 'bullmq' +import { emailQueue } from '../../src/queues'; +import connection from '../../src/queues/connection'; + +jest.mock('bullmq', () => ({ + ...jest.requireActual("bullmq"), + Worker: jest.fn().mockImplementation((queueName, processingFunction, options) => { + const workerMock = { + on: jest.fn(), + }; + return workerMock; + }), +})); + +describe('email.worker', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should create a new BullMQ worker for emailQueue', async () => { + require('../../src/workers/email.worker') + expect(Worker).toHaveBeenCalledWith( + emailQueue.name, + expect.any(Function), + { connection } + ); + }); +}); diff --git a/package.json b/package.json index 0f006cd..ca7dcb9 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 --silent", + "test": "env-cmd jest --coverage", "prettier": "npx prettier --write .", "lint": "npx eslint ." }, diff --git a/src/jobs/email.job.ts b/src/jobs/email.job.ts new file mode 100644 index 0000000..dfd6b00 --- /dev/null +++ b/src/jobs/email.job.ts @@ -0,0 +1,23 @@ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ +import { type Job } from 'bullmq'; + +const emailJob = async (job: Job): Promise => { + // Define the processing logic for email jobs + const { to, subject, body } = job.data; // Assuming the job data contains the recipient, subject, and body of the email + + try { + // Perform email processing logic here + console.log(`Sending email to ${to}: ${subject}`); + console.log('Body:', body); + + // Simulating email sending time + await new Promise((resolve) => setTimeout(resolve, 2000)); + + console.log('Email sent successfully'); + } catch (error) { + console.error('Error processing email:', error); + throw new Error('Failed to process email'); // Throw an error if email processing fails + } +} + +export default emailJob \ No newline at end of file diff --git a/src/workers/email.worker.ts b/src/workers/email.worker.ts index 26b3df3..877232a 100644 --- a/src/workers/email.worker.ts +++ b/src/workers/email.worker.ts @@ -2,41 +2,9 @@ import { Worker } from 'bullmq'; import { emailQueue } from '../queues'; // Assuming you have already defined the emailQueue import connection from '../queues/connection'; +import emailJob from '../jobs/email.job'; // Create a new BullMQ worker instance for emailQueue -const emailWorker = new Worker(emailQueue.name, async (job) => { - // Define the processing logic for email jobs - const { to, subject, body } = job.data; // Assuming the job data contains the recipient, subject, and body of the email - - try { - // Perform email processing logic here - console.log(`Sending email to ${to}: ${subject}`); - console.log('Body:', body); - - // Simulating email sending time - await new Promise((resolve) => setTimeout(resolve, 2000)); - - console.log('Email sent successfully'); - } catch (error) { - console.error('Error processing email:', error); - throw new Error('Failed to process email'); // Throw an error if email processing fails - } -}, { connection }); - -emailWorker.on('failed', (job, err) => { - if (job !== undefined) { - console.error(`Job ${job.id} failed with error:`, err); - } else { - console.error('The job was undefined.') - } -}); - -emailWorker.on('completed', (job) => { - console.log(`Job ${job.id} completed successfully`); -}); - -emailWorker.on('error', (error) => { - console.error('Worker error:', error); -}); +const emailWorker = new Worker(emailQueue.name, emailJob, { connection }); export default emailWorker \ No newline at end of file