【问题标题】:Mocking Bull queues in NestJS在 NestJS 中模拟公牛队列
【发布时间】:2022-04-21 03:26:27
【问题描述】:

我正在尝试测试在向我的一个控制器发送请求后,队列会推送一个作业。实现本身按预期工作。

这是我的 app.module.ts

    @Module({
    imports: [
    HttpModule,
    TypeOrmModule.forRoot(typeOrmConfig),
    BullModule.forRoot({
      redis: {
        host: redisConfig.host,
        port: redisConfig.port,
      },
    }),
    // Bunch of unrelated modules
     ],
     providers: [
    {
      provide: APP_FILTER,
      useClass: AllExceptionsFilter,
    },
    ],
     controllers: [SomeControllers],
    })
    export class AppModule {}

这就是我的 import.module.ts(使用队列的模块)的样子:

@Module({
  imports: [
    BullModule.registerQueue({
      name: importQueueName.value,
    }),
   //More unrelated modules,
  ],
  providers: [
    //More services, and bull consumer and producer,
    ImportDataProducer,
    ImportDataConsumer,
    ImportDataService,
  ],
  controllers: [ImportDataController],
})
export class ImportDataModule {}

正如我所说,从功能上讲,实现效果很好

我尝试关注this approach

没有在 beforeAll 钩子中注册队列,我得到了

 Driver not Connected

还有this approach

在测试套件的 beforeAll 挂钩中注册了一个队列,我得到:

 TypeError: Cannot read properties of undefined (reading 'call')

      at BullExplorer.handleProcessor (node_modules/@nestjs/bull/dist/bull.explorer.js:95:23)
      at MapIterator.iteratee (node_modules/@nestjs/bull/dist/bull.explorer.js:59:26)
      at MapIterator.next (node_modules/iterare/src/map.ts:9:39)
      at FilterIterator.next (node_modules/iterare/src/filter.ts:11:34)
      at IteratorWithOperators.next (node_modules/iterare/src/iterate.ts:19:28)
          at Function.from (<anonymous>)
      at IteratorWithOperators.toArray (node_modules/iterare/src/iterate.ts:227:22)
      at MetadataScanner.scanFromPrototype (node_modules/@nestjs/core/metadata-scanner.js:12:14)
      at node_modules/@nestjs/bull/dist/bull.explorer.js:56:34
          at Array.forEach (<anonymous>)

这是我的“基础测试套件”:

describe('Queue test suite', () => {
  let app: INestApplication;
  const importQueue: any = { add: jest.fn() };
  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule, ImportDataModule],
    })
      .overrideProvider(importQueueName.value)
      .useValue(importQueue)
      .compile();

    app = moduleFixture.createNestApplication();
    app.useGlobalPipes(
      new ValidationPipe({
        transform: true,
        whitelist: true,
        forbidNonWhitelisted: true,
      }),
    );
    await app.init();
  });

  afterAll(async () => {
   
    await app.close();
 
  });

  test('A job should be pushed', async () => {
    await request(app.getHttpServer())
      .post('/some/route')
      .attach('file', __dirname + '/some.file')
      .expect(HttpStatus.CREATED);
   

    expect(importQueue.add).toHaveBeenCalled();
  });
});

您知道这里可能出了什么问题吗?谢谢!

【问题讨论】:

    标签: testing mocking nestjs bullmq


    【解决方案1】:

    我遇到了同样的问题,问题出在您的mockQueue 上。您需要添加一个 process 模拟函数。

    这应该适合你!

    const importQueue: any = { 
      add: jest.fn(),
      process: jest.fn(),
    };
    

    这就是我测试它的方式。

    expect(mockQueue.add).toBeCalledTimes(1);
          expect(mockQueue.add).nthCalledWith(
            1,
            PendoJobNames.SCR,
            {
              ...mockJobDto,
            },
            {
              jobId: mockDto.visitorId,
              removeOnComplete: true,
              removeOnFail: true,
            },
          );
        ```
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2019-12-19
      • 2019-10-08
      • 2019-09-06
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2011-02-15
      • 2022-07-08
      相关资源
      最近更新 更多