import { AmqpConnection , RabbitMQModule , RabbitSubscribe , } from '@golevelup/nestjs-rabbitmq' ; import { INestApplication , Injectable } from '@nestjs/common' ; import { Test } from '@nestjs/testing' ; const testHandler = jest . fn ( ) ; const exchange = 'testSubscribeNoHandlerExhange' ; const routingKey1 = 'testSubscribeNoHandlerRoute1' ; const routingKey2 = 'testSubscribeNoHandlerRoute2' ; @ Injectable ( ) class SubscribeService { @ RabbitSubscribe ( { exchange , routingKey : [ routingKey1 , routingKey2 ] , queue : 'subscribeNoHandlerQueue' , } ) handleSubscribe ( message ) { testHandler ( message ) ; } } describe ( 'Rabbit Subscribe Without Register Handlers' , ( ) => { let app ; let amqpConnection ; const rabbitHost = process . env . NODE_ENV === 'ci' ? process . env . RABBITMQ_HOST : 'localhost' ; const rabbitPort = process . env . NODE_ENV === 'ci' ? process . env . RABBITMQ_PORT : '5672' ; const uri = `amqp://rabbitmq:rabbitmq@${ rabbitHost }:${ rabbitPort }` ; beforeAll ( async ( ) => { const moduleFixture = await Test . createTestingModule ( { providers : [ SubscribeService ] , imports : [ RabbitMQModule . forRoot ( RabbitMQModule , { exchanges : [ { name : exchange , type : 'topic' , } , ] , uri , connectionInitOptions : { wait : true , reject : true , timeout : 3000 } , registerHandlers : false , } ) , ] , } ) . compile ( ) ; app = moduleFixture . createNestApplication ( ) ; amqpConnection = app . get < AmqpConnection > ( AmqpConnection ) ; await app . init ( ) ; } ) ; afterAll ( async ( ) => { await app . close ( ) ; } ) ; it ( 'should not receive subscribe messages because register handlers is disabled' , async ( done ) => { [ routingKey1 , routingKey2 ] . forEach ( ( x , i ) => amqpConnection . publish ( exchange , x , `testMessage-${ i }` ) ) ; expect . assertions ( 1 ) ; setTimeout ( ( ) => { expect ( testHandler ) . not . toHaveBeenCalled ( ) ; done ( ) ; } , 100 ) ; } ) ; } ) ;