Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"language": "en",
"words": ["fizzbuds"]
"words": ["fizzbuds", "rabbitmq", "testcontainers"]
}
9 changes: 0 additions & 9 deletions packages/ddd-toolkit-rabbit-bus/docker-compose.yml

This file was deleted.

4 changes: 3 additions & 1 deletion packages/ddd-toolkit-rabbit-bus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lint-staged": "^14.0.1",
"prettier": "^3.1.1",
"rimraf": "^5.0.5",
"testcontainers": "^11.7.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
Expand All @@ -64,6 +65,7 @@
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"testTimeout": 90000
}
}
65 changes: 59 additions & 6 deletions packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { Event, IEventHandler, ILogger, waitFor } from '@fizzbuds/ddd-toolkit';
import { RabbitEventBus } from './index';

// Conditionally import testcontainers only for local development
let GenericContainer: any, Wait: any;
const isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';

if (!isCI) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const testcontainers = require('testcontainers');
GenericContainer = testcontainers.GenericContainer;
Wait = testcontainers.Wait;
} catch (error) {
console.warn('Testcontainers not available, falling back to localhost RabbitMQ');
}
}

const loggerMock: ILogger = {
log: jest.fn(),
debug: jest.fn(),
Expand All @@ -20,17 +35,55 @@ class BarEvent extends Event<{ bar: string }> {
}
}

describe('RabbitEventBus', () => {
beforeEach(() => jest.resetAllMocks());
const shouldRunTests = isCI || GenericContainer;

(shouldRunTests ? describe : describe.skip)('RabbitEventBus', () => {
let container: any;
let rabbitEventBus: RabbitEventBus;
let rabbitUrl: string;

beforeAll(async () => {
if (isCI) {
// Use GitHub Actions RabbitMQ service
rabbitUrl = 'amqp://guest:guest@localhost:5672';
console.log('Using GitHub Actions RabbitMQ service');
} else {
// Use testcontainers for local development
console.log('Starting RabbitMQ container with testcontainers');
container = await new GenericContainer('rabbitmq:3.8-management')
.withEnvironment({
RABBITMQ_DEFAULT_USER: 'guest',
RABBITMQ_DEFAULT_PASS: 'guest',
})
.withExposedPorts(5672, 15672)
.withWaitStrategy(Wait.forListeningPorts())
.start();

const mappedPort = container.getMappedPort(5672);
rabbitUrl = `amqp://guest:guest@localhost:${mappedPort}`;
}
}, 60000); // 60 second timeout for container startup

afterAll(async () => {
if (container && container.stop) {
await container.stop();
}
});

beforeEach(() => {
jest.resetAllMocks();
});

beforeEach(async () => {
rabbitEventBus = new RabbitEventBus('amqp://guest:guest@localhost', 'exchange', 10, 3, undefined, loggerMock);
rabbitEventBus = new RabbitEventBus(rabbitUrl, 'exchange', 10, 3, undefined, loggerMock);
await rabbitEventBus.init();
});

afterEach(async () => await rabbitEventBus.terminate());
afterEach(async () => {
if (rabbitEventBus) {
await rabbitEventBus.terminate();
}
});

describe('Given an handler subscribed to an event', () => {
const handlerMock = jest.fn();
Expand Down Expand Up @@ -89,7 +142,7 @@ describe('RabbitEventBus', () => {
await rabbitEventBus.publish(new FooEvent({ foo: 'foo' }));

await waitFor(() => expect(handlerMock).not.toBeCalled());
})
});
});
});

Expand Down Expand Up @@ -181,7 +234,7 @@ describe('RabbitEventBus', () => {

describe('Given no handler subscribed', () => {
class FooEventHandler implements IEventHandler<FooEvent> {
async handle() { }
async handle() {}
}

beforeEach(async () => {
Expand Down
Loading