import { AmqpConnection } from '@golevelup/nestjs-rabbitmq' ; import { INestApplication } from '@nestjs/common' ; import { Test } from '@nestjs/testing' ; import { AppModule } from '../src/app.module' ; const nonJsonRpcRoutingKey = 'non-json-rpc' ; describe ( 'Rabbit RPC' , ( ) => { let app ; let amqpConnection ; afterEach ( ( ) => { jest . clearAllMocks ( ) ; } ) ; afterAll ( async ( ) => { await app . close ( ) ; } ) ; beforeAll ( async ( ) => { const moduleFixture = await Test . createTestingModule ( { imports : [ AppModule ] , } ) . compile ( ) ; app = moduleFixture . createNestApplication ( ) ; amqpConnection = app . get < AmqpConnection > ( AmqpConnection ) ; await app . init ( ) ; } ) ; it ( 'regular RPC handler should receive a valid RPC response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : 'rpc' , payload : { request : 'val' , } , } ) ; expect ( response ) . toEqual ( { echo : { request : 'val' } } ) ; } ) ; it ( 'intercepted RPC handler should receive a transformed RPC response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : 'intercepted-rpc' , } ) ; expect ( response ) . toEqual ( { transformed : { message : 42 } } ) ; } ) ; it ( 'error reply RPC handler with non-JSON message should return an RPC error response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : 'error-reply-rpc' , payload : Buffer . from ( '{a:' ) , } ) ; expect ( response ) . toHaveProperty ( 'message' ) ; expect ( response ) . toMatchObject ( { status : 'error' } ) ; } ) ; it ( 'non-JSON RPC handler with should receive a valid RPC response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : nonJsonRpcRoutingKey , payload : { request : 'val' , } , } ) ; expect ( response ) . toEqual ( { echo : { request : 'val' } } ) ; } ) ; it ( 'non-JSON RPC handler with undefined message should receive a valid RPC response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : nonJsonRpcRoutingKey , payload : Buffer . alloc ( 0 ) , } ) ; expect ( response ) . toEqual ( { echo : undefined } ) ; } ) ; it ( 'non-JSON RPC handler with unparsable message should receive a valid RPC response' , async ( ) => { const response = await amqpConnection . request ( { exchange : 'exchange1' , routingKey : nonJsonRpcRoutingKey , payload : Buffer . from ( '{a:' ) , } ) ; expect ( response ) . toEqual ( { echo : undefined } ) ; } ) ; } ) ;