Given an Injectable that uses a queue via the @InjectQueue decorator:
@Injectable()
export class EnqueuerService {
constructor (
@InjectQueue(QUEUE_NAME) private readonly queue: Queue
) {
}
async foo () {
return this.queue.add('job')
}
}
How can I test that this service calls the queue correctly? I can do the bsic scaffolding:
describe('EnqueuerService', () => {
let module: TestingModule
let enqueuerService: EnqueuerService
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [EnqueuerModule]
}).compile()
enqueuerService = module.get(EnqueuerService)
// Here I'd usually pull in the dependency to test against:
// queue = module.get(QUEUE_NAME)
//
// (but this doesn't work because queue is using the @InjectQueue decorator)
})
afterAll(async () => await module.close())
describe('#foo', () => {
it('adds a job', async () => {
await enqueuerService.foo()
// Something like this would be nice:
// expect(queue.add).toBeCalledTimes(1)
//
// (but maybe there are alternative ways that are easier?)
})
})
})
I'm quite lost in the Nest DI container setup but I suspect there's some clever way of doing this. But despite hours of attempts I can't make progress, and the documentation isn't helping me. Can anyone offer a solution? It doesn't have to be mocking, if it's easier to create a real queue to test against that's fine too I just want to verify my service enqueues as expected! Any help appreciated.
Error: Nest could not find queue element
, it comes from themodule.get(QUEUE_NAME)
line. FWIW if I remove the "queue" references entirely the test runs (albeit with no assertions), so there is something here that works, it feels very close to working fully. – Dialogism