本文介绍了如何通过Jest(DI via@InjectQueue)测试Nest Bull队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个通过@InjectQueue修饰符使用队列的可注入对象:

@Injectable()
export class EnqueuerService {
  constructor (
    @InjectQueue(QUEUE_NAME) private readonly queue: Queue
  ) {
  }

  async foo () {
    return this.queue.add('job')
  }
}
如何测试此服务是否正确调用队列?我可以做bsic脚手架:

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?)
    })
  })
})

我完全迷失在Nest DI容器设置中,但我怀疑有一些聪明的方法可以做到这一点。但是,尽管尝试了几个小时,我还是不能取得进展,文档对我也没有帮助。有人能提供解决方案吗?它不需要模拟,如果创建一个真实的队列进行测试更容易,那也没问题,我只想按预期验证我的服务队列!感谢您的任何帮助。

推荐答案

通过查看lib,我找到了一个helper function根据其名称创建队列注入令牌:getQueueToken(name?: string)

该函数是从库中导出的,因此您可以使用它为测试提供您自己的队列实现。

import { getQueueToken } from '@nestjs/bull';

describe('EnqueuerService', () => {
  let module: TestingModule
  let enqueuerService: EnqueuerService

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [EnqueuerModule]
    })
      .overrideProvider(getQueueToken(QUEUE_NAME))
      .useValue({ /* mocked queue */ })
      .compile()

    enqueuerService = module.get(EnqueuerService)
    queue = module.get(QUEUE_NAME)
  })

  afterAll(async () => await module.close())

  describe('#foo', () => {
    it('adds a job', async () => {
      await enqueuerService.foo()

      expect(queue.add).toBeCalledTimes(1)
    })
  })
})

编辑

要检查队列服务调用的方法,您可以在测试文件的根目录创建一个mock。我重新组织了测试文件,使测试设置和测试本身之间更加清晰:

let module: TestingModule
let enqueuerService: EnqueuerService
let mockQueue;

// Create a helper function to create the app.
const createApp = async () => {
  module = await Test.createTestingModule({
    imports: [EnqueuerModule]
  })
    .overrideProvider(getQueueToken(QUEUE_NAME))
    .useValue(mockQueue)
    .compile()

  enqueuerService = module.get(EnqueuerService)
}

describe('EnqueuerService', () => {
  // Recreate app before each test to clean the mock.
  beforeEach(async () => {
    mockQueue = {
      add: jest.fn(),
    };
    await createApp();
  })

  afterAll(async () => await module.close())

  describe('#foo', () => {
    it('adds a job', async () => {
      await enqueuerService.foo()

      // Check calls on the mock.
      expect(queue.add).toBeCalledTimes(1)
    })
  })

  describe('#bar', () => {
    // Override the mock for a specific test suite.
    beforeEach(async () => {
      mockQueue.add = jest.fn().mockImplementation(/** */);
      // The mock has changed so we need to recreate the app to use the new value.
      await createApp();
    })

    it('adds a job', async () => {
      // ...
    })
  })
})

这篇关于如何通过Jest(DI via@InjectQueue)测试Nest Bull队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 07:46