-
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.
- Loading branch information
Showing
5 changed files
with
93 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: '[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,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 } | ||
); | ||
}); | ||
}); |
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,23 @@ | ||
/* eslint-disable @typescript-eslint/restrict-template-expressions */ | ||
import { type Job } from 'bullmq'; | ||
|
||
const emailJob = async (job: Job): Promise<void> => { | ||
// 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 |
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