Skip to content

amqpx v1.0.1


amqpx / extensions/jest / TestConsumer

Class: TestConsumer<T>

Defined in: src/extensions/jest/test-consumer.ts:34

Test implementation of Consumer using jest mocks.

listen method instantly returns current instance wrapped in Promise. You can assert the parameters with which was this method called to assert listen method was called.

queue and channel properties are new instances of TestQueue and TestChannel respectively. All other methods are jest mocks returning void or the current instance, depending on the method semantic.

Example

ts
import { TestConsumer } from 'amqpx/jest';

const consumer = new TestConsumer();

const channel = consumer.getChannel();

expect(channel).toBeInstanceOf(TestChannel);

const listener = () => { / * empty * / };
await consumer.listen(listener);

expect(consumer.listen).toBeCalledWith(listener);

Extends

  • EventEmitter

Type Parameters

Type Parameter
T

Implements

Constructors

Constructor

new TestConsumer<T>(): TestConsumer<T>

Defined in: src/extensions/jest/test-consumer.ts:36

Returns

TestConsumer<T>

Overrides

EventEmitter.constructor

Properties

PropertyTypeDescriptionDefined in
channelTestChannelThe channel used by this consumer.src/extensions/jest/test-consumer.ts:49
closeMock<any, any, any>Stops consuming new messages and waits for all in-flight message handlers to finish. Param Maximum time in milliseconds to wait for in-flight messages to complete. Defaults to 30000.src/extensions/jest/test-consumer.ts:41
listenMock<any, any, any>Registers the message handler and starts consuming from the queue. Param Handler invoked for each received message.src/extensions/jest/test-consumer.ts:43
queueTestQueueThe queue this consumer reads from.src/extensions/jest/test-consumer.ts:47
setPrefetchMock<any, any, any>-src/extensions/jest/test-consumer.ts:45

Methods

deliverMessage()

deliverMessage(message, options?): Promise<void>

Defined in: src/extensions/jest/test-consumer.ts:57

Simulates a message arriving from RabbitMQ by invoking the most recently registered listener. Throws if listen has not been called yet.

Parameters

ParameterTypeDescription
messageTThe parsed message payload to deliver.
optionsobject & Omit<Partial<ConsumeMessage>, "content">Optional serialization override and raw amqplib message field overrides.

Returns

Promise<void>

Released under the MIT License.