Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs bullmq worker processor don't works with jest #1772

Closed
2 of 4 tasks
laurentvandelle opened this issue Aug 8, 2023 · 1 comment
Closed
2 of 4 tasks

Nestjs bullmq worker processor don't works with jest #1772

laurentvandelle opened this issue Aug 8, 2023 · 1 comment
Labels
bug Something isn't working needs triage

Comments

@laurentvandelle
Copy link

laurentvandelle commented Aug 8, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The test is still running with

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.

Steps to reproduce

Step to reproduce

  1. Create a new nest project : nest new nest-app
  2. Install dependencies : npm i @nestjs/bullmq ioredis
  3. Start a redis instance on default port 6379
  4. Create this 2 files

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();
  });
});

Expected behavior

The test just succeed

Package version

10.0.1

Bull version

4.7.0

NestJS version

10.0.0

Node.js version

16.20.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

npm : 9.8.1

@laurentvandelle laurentvandelle added bug Something isn't working needs triage labels Aug 8, 2023
@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@nestjs nestjs locked and limited conversation to collaborators Aug 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants