thi*_*ign 4 testing queue jestjs nestjs bull
我通过 Bull 使用 NestJS 队列并在 Jest 中编写单元测试。我想跳过测试期间触发的作业的处理。
Bull 的测试模式存在 GH 问题,但他们不会实施它。
我最好避免大量的嘲笑,一个简单的选项BullModule是最好的。
小智 7
我也遇到了困难,所以我发布了我的解决方案。我只是为此目的简化了我的代码,因此请谨慎使用,可能会有一些拼写错误等。
describe('JobCreatorService', () => {
let service: JobCreatorService;
let moduleRef: TestingModule;
const exampleQueueMock = { add: jest.fn() };
beforeEach(async () => {
jest.resetAllMocks();
moduleRef = await Test.createTestingModule({
imports: [
BullModule.registerQueue({
name: EXAMPLE_QUEUE,
}),
],
})
.overrideProvider(getQueueToken(EXAMPLE_QUEUE))
.useValue(exampleQueueMock)
.compile();
service = moduleRef.get<JobCreatorService>(JobCreatorService);
});
afterAll(async () => {
await moduleRef.close();
});
it('should dispatch job', async () => {
await service.methodThatDispatches();
expect(exampleQueueMock.add).toHaveBeenCalledWith({example: jobData});
});
});
Run Code Online (Sandbox Code Playgroud)
我不建议进行具有副作用的单元测试 - 例如将数据存储在数据库或 redis 中,这就是这种方法的原因。但是,如果您更喜欢将作业实际存储在 Redis 中,您可能会在这里找到一些灵感: https: //github.com/nestjs/bull/blob/master/e2e/module.e2e-spec.ts
| 归档时间: |
|
| 查看次数: |
3957 次 |
| 最近记录: |