Testing utilities for AMQP contracts with testcontainers
npm install @amqp-contract/testingTesting utilities for AMQP contracts using testcontainers.





- š³ Automatically starts RabbitMQ container for tests
- ā
Works with Vitest globalSetup
- š Fast and reliable integration testing
- š Includes RabbitMQ management console
``bash`
pnpm add -D @amqp-contract/testing
Add to your vitest.config.ts:
`typescript
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globalSetup: ["@amqp-contract/testing/global-setup"],
},
});
`
For TypeScript projects, reference the type definitions in your tsconfig.json:
`json`
{
"compilerOptions": {
"types": ["@amqp-contract/testing/types/vitest"]
}
}
Or add a triple-slash reference in your test files:
`typescript`
///
This provides type-safe access to the test container context variables.
The package provides a Vitest extension that automatically manages RabbitMQ connections:
`typescript
import { describe, expect } from "vitest";
import { it } from "@amqp-contract/testing/extension";
describe("Order Processing", () => {
it("should publish and consume messages", async ({ amqpConnection }) => {
// amqpConnection is automatically provided and cleaned up
// Your test code here using amqpConnection
});
});
`
The extension provides:
- vhost: A unique virtual host created for test isolation (automatically cleaned up after the test)amqpConnectionUrl
- : A connection URL pre-configured with the test vhostamqpConnection
- : An established connection to the RabbitMQ testcontaineramqpChannel
- : A channel for AMQP operationspublishMessage
- : Helper function for publishing test messagesinitConsumer
- : Helper function for setting up test consumers
- Automatic connection and vhost cleanup after each test
Global Setup:
1. Starts a RabbitMQ container with management plugin
2. Waits for RabbitMQ to be healthy
3. Provides connection details to your tests
4. Cleans up the container after tests complete
Test Extension:
1. Creates a unique virtual host (vhost) for each test to ensure complete isolation
2. Provides pre-configured connections and helpers for interacting with RabbitMQ
3. Automatically cleans up the vhost and connections after each test completes
- Image: rabbitmq:4.2.1-management-alpine (default)RABBITMQ_IMAGE
- Can be configured via environment variableguest
- Ports:
- 5672 (AMQP)
- 15672 (Management console)
- Credentials:
- User: guest
- Password:
- RABBITMQ_IMAGE - Docker image to use for the RabbitMQ containerrabbitmq:4.2.1-management-alpine
- Default:
- Can be set to any compatible RabbitMQ image with management plugin
The following variables are provided to tests:
- __TESTCONTAINERS_RABBITMQ_IP__ - Container host IP__TESTCONTAINERS_RABBITMQ_PORT_5672__
- - Mapped AMQP port__TESTCONTAINERS_RABBITMQ_PORT_15672__` - Mapped management port
-
š Read the full documentation ā
MIT