import dotenv from 'dotenv'; dotenv.config(); let cfg = require('../../src/config'); import amqplib from 'amqplib'; import { RabbitMQ } from '../../src/lambda'; import { Connection, Channel } from 'amqplib'; describe('RabbitMQ', () => { let rabbitmq: RabbitMQ; let connectionMock: jest.Mocked; let channelMock: jest.Mocked; beforeEach(() => { // Initialize the rabbitmq instance rabbitmq = new RabbitMQ(); // Create a mock connection object connectionMock = { createChannel: jest.fn().mockResolvedValue({ assertExchange: jest.fn().mockResolvedValue(null), }), } as any; // Create a mock channel object using the mock connection channelMock = connectionMock.createChannel(); // Spy on the amqplib.connect function and make it return the mock connection jest.spyOn(amqplib, 'connect').mockResolvedValue(connectionMock); }); afterEach(() => { // Restore all mocks jest.restoreAllMocks(); // Re-require the config module cfg = require('../../src/config'); }); test('should create a connection and channel', async () => { // Act // Connect to RabbitMQ using the URL and exchange name from the config await rabbitmq.connect( cfg.default.rabbitmq.url, //TODO:change cfg.default.rabbitmq.url to cfg.rabbitmq.url cfg.default.rabbitmq.globalEventChannel.exchangeName, ); // Assert // Check that the connection and channel were created expect(rabbitmq.connection).toBe(connectionMock); expect(connectionMock.createChannel).toHaveBeenCalled(); // Check that the amqplib.connect function was called with the correct arguments expect(amqplib.connect).toHaveBeenCalledWith(cfg.default.rabbitmq.url, { reconnectTimeInSeconds: 10, reconnectLimit: 12, }); }); });