11import { Event , IEventHandler , ILogger , waitFor } from '@fizzbuds/ddd-toolkit' ;
22import { RabbitEventBus } from './index' ;
3- import { GenericContainer , StartedTestContainer , Wait } from 'testcontainers' ;
3+
4+ // Conditionally import testcontainers only for local development
5+ let GenericContainer : any , Wait : any ;
6+ const isCI = process . env . CI === 'true' || process . env . GITHUB_ACTIONS === 'true' ;
7+
8+ if ( ! isCI ) {
9+ try {
10+ // eslint-disable-next-line @typescript-eslint/no-var-requires
11+ const testcontainers = require ( 'testcontainers' ) ;
12+ GenericContainer = testcontainers . GenericContainer ;
13+ Wait = testcontainers . Wait ;
14+ } catch ( error ) {
15+ console . warn ( 'Testcontainers not available, falling back to localhost RabbitMQ' ) ;
16+ }
17+ }
418
519const loggerMock : ILogger = {
620 log : jest . fn ( ) ,
@@ -21,28 +35,37 @@ class BarEvent extends Event<{ bar: string }> {
2135 }
2236}
2337
24- describe ( 'RabbitEventBus' , ( ) => {
25- let container : StartedTestContainer ;
38+ const shouldRunTests = isCI || GenericContainer ;
39+
40+ ( shouldRunTests ? describe : describe . skip ) ( 'RabbitEventBus' , ( ) => {
41+ let container : any ;
2642 let rabbitEventBus : RabbitEventBus ;
2743 let rabbitUrl : string ;
2844
2945 beforeAll ( async ( ) => {
30- // Start RabbitMQ container
31- container = await new GenericContainer ( 'rabbitmq:3.8-management' )
32- . withEnvironment ( {
33- RABBITMQ_DEFAULT_USER : 'guest' ,
34- RABBITMQ_DEFAULT_PASS : 'guest' ,
35- } )
36- . withExposedPorts ( 5672 , 15672 )
37- . withWaitStrategy ( Wait . forListeningPorts ( ) )
38- . start ( ) ;
39-
40- const mappedPort = container . getMappedPort ( 5672 ) ;
41- rabbitUrl = `amqp://guest:guest@localhost:${ mappedPort } ` ;
46+ if ( isCI ) {
47+ // Use GitHub Actions RabbitMQ service
48+ rabbitUrl = 'amqp://guest:guest@localhost:5672' ;
49+ console . log ( 'Using GitHub Actions RabbitMQ service' ) ;
50+ } else {
51+ // Use testcontainers for local development
52+ console . log ( 'Starting RabbitMQ container with testcontainers' ) ;
53+ container = await new GenericContainer ( 'rabbitmq:3.8-management' )
54+ . withEnvironment ( {
55+ RABBITMQ_DEFAULT_USER : 'guest' ,
56+ RABBITMQ_DEFAULT_PASS : 'guest' ,
57+ } )
58+ . withExposedPorts ( 5672 , 15672 )
59+ . withWaitStrategy ( Wait . forListeningPorts ( ) )
60+ . start ( ) ;
61+
62+ const mappedPort = container . getMappedPort ( 5672 ) ;
63+ rabbitUrl = `amqp://guest:guest@localhost:${ mappedPort } ` ;
64+ }
4265 } , 60000 ) ; // 60 second timeout for container startup
4366
4467 afterAll ( async ( ) => {
45- if ( container ) {
68+ if ( container && container . stop ) {
4669 await container . stop ( ) ;
4770 }
4871 } ) ;
0 commit comments