amqpx / extensions/jest / TestBatchConsumer
Class: TestBatchConsumer<T>
Defined in: src/extensions/jest/test-batch-consumer.ts:35
Test implementation of BatchConsumer 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 return new instances of TestQueue and TestChannel respectively. close method is a jest mock returning void.
Use deliverMessages to simulate a batch of messages arriving and invoke the registered listener.
Example
import { TestBatchConsumer } from 'amqpx/jest';
const consumer = new TestBatchConsumer<{ id: number }>();
const handler = jest.fn();
await consumer.listen(handler);
await consumer.deliverMessages([{ id: 1 }, { id: 2 }]);
expect(handler).toHaveBeenCalledTimes(1);Extends
EventEmitter
Type Parameters
| Type Parameter |
|---|
T |
Implements
Constructors
Constructor
new TestBatchConsumer<
T>():TestBatchConsumer<T>
Defined in: src/extensions/jest/test-batch-consumer.ts:37
Returns
TestBatchConsumer<T>
Overrides
EventEmitter.constructor
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
channel | TestChannel | The channel used by this consumer. | src/extensions/jest/test-batch-consumer.ts:48 |
close | Mock<any, any, any> | Stops consuming new messages and waits for all in-flight batches to finish processing. Param Maximum time in milliseconds to wait for in-flight messages to complete. Defaults to 30000. | src/extensions/jest/test-batch-consumer.ts:42 |
listen | Mock<any, any, any> | Registers the batch handler and starts consuming from the queue. Param Handler invoked with a batch of received messages. | src/extensions/jest/test-batch-consumer.ts:44 |
queue | TestQueue | The queue this consumer reads from. | src/extensions/jest/test-batch-consumer.ts:46 |
Methods
deliverMessages()
deliverMessages(
messages,options?):Promise<void>
Defined in: src/extensions/jest/test-batch-consumer.ts:56
Simulates a batch of messages arriving from RabbitMQ by invoking the most recently registered listener. Throws if listen has not been called yet.
Parameters
| Parameter | Type | Description |
|---|---|---|
messages | T[] | Array of parsed message payloads to deliver as a single batch. |
options | object & Omit<Partial<ConsumeMessage>, "content"> | Optional serialization override and raw amqplib message field overrides applied to each message. |
Returns
Promise<void>