Skip to content

Instantly share code, notes, and snippets.

@lazarok09
Last active September 2, 2025 13:40
Show Gist options
  • Select an option

  • Save lazarok09/3ee3e27357a4f8b869608c45ff17a9f6 to your computer and use it in GitHub Desktop.

Select an option

Save lazarok09/3ee3e27357a4f8b869608c45ff17a9f6 to your computer and use it in GitHub Desktop.
This Gist demonstrates how to create type-safe DTOs for BullMQ jobs in TypeScript and validate them in Jest tests using Supertest
/*
This Gist demonstrates how to create type-safe DTOs for BullMQ jobs in TypeScript and validate them in Jest tests using Supertest.
By extending job payloads and responses with DTOs, you can ensure strong typing while testing your NestJS webhooks and background jobs.
The example shows how to:
Define a job DTO (SendMilionsOfEmailsDTO) and response DTO (SendMilionsOfEmailsResponseDTO).
Infer the correct job type from bullmq.Job.toJSON.
Write Jest + Supertest assertions for job processing results, including attemptsMade, finishedOn, processedOn, and typed returnvalue.
Validate nested DTOs (email, name) using expect.objectContaining.
This approach improves test reliability, maintainability, and TypeScript safety when working with BullMQ queues in Node.js/NestJS
applications.
๐Ÿ”‘ Keywords (for SEO)
typescript bullmq types for failed and success jobs
BullMQ TypeScript Example
BullMQ Jest Supertest
NestJS BullMQ Job Testing
TypeScript DTO BullMQ
BullMQ Queue Testing Example
Type-Safe BullMQ Jobs
Jest Supertest BullMQ Integration
BullMQ Job toJSON TypeScript
๐Ÿ“š Tags for Gist
#typescript #nestjs #bullmq #jest #supertest #queues #testing #dto #jobs
- Little Docs generated with IA, GPT-5
*/
import { Job } from 'bullmq';
import * as request from 'supertest';
// You're gonna need your DTO and the response.
class User {
email!: string;
name!: string;
}
export class SendMilionsOfEmailsDTO {
userEmail!: string;
}
export class SendMilionsOfEmailsResponseDTO extends User {}
export type ProcessSendMilionsOfEmailsToAUserDTO = ReturnType<
Job<SendMilionsOfEmailsDTO, SendMilionsOfEmailsResponseDTO>['toJSON']
>;
// And you can expect in a test file the following assertion:
await request(app.getHttpServer())
.get(`/api/webhooks/jobs/${id}/status`)
.set('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).toMatchObject<ProcessSendMilionsOfEmailsToAUserDTO>({
attemptsMade: expect.any(Number),
attemptsStarted: expect.any(Number),
finishedOn: expect.any(Number),
processedOn: expect.any(Number),
timestamp: expect.any(Number),
queueQualifiedName: expect.any(String),
returnvalue: expect.any(Object),
data: expect.objectContaining<SendMilionsOfEmailsResponseDTO>({
email: expect.any(String),
name: expect.any(String),
}),
opts: expect.any(Object),
progress: expect.any(Number),
stacktrace: expect.any(Array),
delay: expect.any(Number),
priority: expect.any(Number),
stalledCounter: expect.any(Number),
name: expect.any(String),
deferredFailure: expect.any(String),
failedReason: expect.any(String),
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment