When working with an Injectable that utilizes a queue through the @InjectQueue decorator:
@Injectable()
export class EnqueuerService {
constructor (
@InjectQueue(QUEUE_NAME) private readonly queue: Queue
) {
}
async foo () {
return this.queue.add('job')
}
}
I am wondering how I can effectively test if this service interacts with the queue correctly. Setting up the basic structure:
describe('EnqueuerService', () => {
let module: TestingModule
let enqueuerService: EnqueuerService
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [EnqueuerModule]
}).compile()
enqueuerService = module.get(EnqueuerService)
// In a typical case, we would pull in the dependency for testing:
// queue = module.get(QUEUE_NAME)
//
// (but this doesn't work due to the @InjectQueue decorator on queue)
})
afterAll(async () => await module.close())
describe('#foo', () => {
it('adds a job', async () => {
await enqueuerService.foo()
// It would be great to have something like:
// expect(queue.add).toBeCalledTimes(1)
//
// (but there might be simpler alternatives?)
})
})
})
I'm struggling with the Nest DI container configuration and I feel like there must be a smart solution out there. Despite numerous attempts and no luck with the documentation, I'm seeking help from anyone who might have a solution. It's not necessary to use mocking; creating a real queue for testing purposes is also acceptable. My main goal is to ensure that my service queues tasks as intended! Any assistance would be greatly appreciated.