Nestjs bullmq worker处理器不支持jest

5n0oy7gb  于 2023-10-23  发布在  Jest
关注(0)|答案(1)|浏览(155)

复制步骤

1.创建新的嵌套项目:nest new nest-app
1.安装依赖项:npm i @nestjs/bullmq ioredis
1.在默认端口6379上启动redis示例
1.创建这2个文件
app.service.ts

import { Injectable } from '@nestjs/common';
import { Job, Queue, Worker } from 'bullmq';
import Redis, { RedisOptions } from 'ioredis';

@Injectable()
export class AppService {
  private queue: Queue;
  private worker: Worker;
  private redis: Redis;

  connect() {
    const redisConfig: RedisOptions = {
      host: 'localhost',
      port: 6379,
      autoResubscribe: false,
      lazyConnect: true,
      maxRetriesPerRequest: 0,
      reconnectOnError: null,
      enableOfflineQueue: true,
    };
    this.redis = new Redis(redisConfig);
    this.queue = new Queue('queue1', { connection: this.redis });
    this.worker = new Worker(
      'queue1',
      async (job) => {
        console.log('PROCESS')
        await this.wait({});
        console.log('DONE')
        return job;
      },
      { connection: this.redis },
    );
    // Replace by this line and it works
    // this.worker = new Worker('queue1', '', { connection: this.redis });
  }

  async disconnect(): Promise<void> {
    await this.queue.close();
    await this.worker.close();
    await this.redis.quit();
  }

  async wait({ seconds = 1 }: { seconds?: number }): Promise<void> {
    await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
  }
}

app.service.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { AppService } from './app.service';

describe('Queue Service', () => {
  let appService: AppService;

  beforeAll(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [AppService],
    }).compile();
    appService = module.get<AppService>(AppService);
    appService.connect();
  });

  afterAll(async () => {
    await appService.disconnect();
  });

  it('Should pass', async () => {
    expect(true).toBeTruthy();
  });
});

实际结果

测试仍在运行

Jest did not exit one second after the test run has completed.

'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

预期结果

试验刚刚成功

版本号

节点:16.20.1
npm:9.8.1

"dependencies": {
    "@nestjs/bullmq": "^10.0.1",
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "ioredis": "^5.3.2",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.11",
    "@typescript-eslint/parser": "^5.59.11",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^29.5.0",
    "prettier": "^2.8.8",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
bt1cpqcv

bt1cpqcv1#

看起来Jest进程在disconnect()函数promise被解决之前就已经关闭了。
尝试

afterAll(async () => {
  await appService.disconnect();
});

同时将--detectOpenHandles添加到package.json中的jest执行
范例:

"test:e2e": "jest --detectOpenHandles --config ./test/jest-e2e.json"

相关问题