RabbitMQ
Install
| npm install @testcontainers/rabbitmq --save-dev
|
Examples
These examples use the following libraries:
- amqplib
| npm install amqplib
npm install @types/amqplib --save-dev
|
Choose an image from the container registry and substitute IMAGE
.
Produce/consume a message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | const QUEUE = "test";
const PAYLOAD = "Hello World";
await using container = await new RabbitMQContainer(IMAGE).start();
const connection = await amqp.connect(container.getAmqpUrl());
const channel = await connection.createChannel();
await channel.assertQueue(QUEUE);
channel.sendToQueue(QUEUE, Buffer.from(PAYLOAD));
await new Promise((resolve) => {
channel.consume(QUEUE, (message) => {
expect(message?.content.toString()).toEqual(PAYLOAD);
resolve(true);
});
});
await channel.close();
await connection.close();
|
With credentials
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | const USER = "user";
const PASSWORD = "password";
await using container = await new RabbitMQContainer(IMAGE)
.withEnvironment({
RABBITMQ_DEFAULT_USER: USER,
RABBITMQ_DEFAULT_PASS: PASSWORD,
})
.start();
const connection = await amqp.connect({
username: USER,
password: PASSWORD,
port: container.getMappedPort(5672),
});
|