Skip to content

Commit dbac0ab

Browse files
committed
✅ test: rabbitMQ 테스트 컨테이너 설정 및 정리 로직 추가
1 parent 61d7966 commit dbac0ab

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

server/test/integration-test-global-setup.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { MySqlContainer, StartedMySqlContainer } from '@testcontainers/mysql';
22
import { RedisContainer, StartedRedisContainer } from '@testcontainers/redis';
3+
import {
4+
RabbitMQContainer,
5+
StartedRabbitMQContainer,
6+
} from '@testcontainers/rabbitmq';
7+
38
const globalAny: any = global;
49

510
export default async () => {
611
console.log('Starting global setup...');
712
await createMysqlContainer();
813
await createRedisContainer();
14+
await createRabbitMQContainer();
915
jwtEnvSetup();
1016
console.log('Global setup completed.');
1117
};
@@ -40,6 +46,18 @@ const createRedisContainer = async () => {
4046
process.env.REDIS_PASSWORD = '';
4147
};
4248

49+
const createRabbitMQContainer = async () => {
50+
console.log('Starting RabbitMQ container...');
51+
const rabbitMQContainer: StartedRabbitMQContainer =
52+
await new RabbitMQContainer('rabbitmq:4.1-management').start();
53+
globalAny.__RABBITMQ_CONTAINER__ = rabbitMQContainer;
54+
55+
process.env.RABBITMQ_HOST = rabbitMQContainer.getHost();
56+
process.env.RABBITMQ_PORT = rabbitMQContainer.getMappedPort(5672).toString();
57+
process.env.RABBITMQ_DEFAULT_USER = 'guest';
58+
process.env.RABBITMQ_DEFAULT_PASS = 'guest';
59+
};
60+
4361
const jwtEnvSetup = () => {
4462
console.log('Starting Jwt Environment...');
4563
process.env.JWT_ACCESS_SECRET = 'temp';

server/test/integration-test-global-teardown.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ export default async () => {
1313
delete globalAny.__REDIS_CONTAINER__;
1414
}
1515

16+
console.log('Stopping RabbitMQ container...');
17+
if (globalAny.__RABBITMQ_CONTAINER__) {
18+
await globalAny.__RABBITMQ_CONTAINER__.stop();
19+
delete globalAny.__RABBITMQ_CONTAINER__;
20+
}
21+
1622
console.log('Global teardown completed.');
1723
};

server/test/jest.setup.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { HttpExceptionsFilter } from '../src/common/filters/http.exception.filte
77
import * as cookieParser from 'cookie-parser';
88
import { TestService } from '../src/common/test/test.service';
99
import { RedisService } from '../src/common/redis/redis.service';
10+
import { RabbitMQService } from '../src/common/rabbitmq/rabbitmq.service';
1011

1112
const globalAny: any = global;
1213

@@ -39,6 +40,8 @@ afterAll(async () => {
3940
await redisService.flushall();
4041
redisService.disconnect();
4142

43+
await cleanupRabbitMQ();
44+
4245
console.log('Closing NestJS application...');
4346
if (globalAny.testApp) {
4447
await globalAny.testApp.close();
@@ -47,6 +50,20 @@ afterAll(async () => {
4750
console.log('NestJS application closed.');
4851
});
4952

53+
async function cleanupRabbitMQ() {
54+
try {
55+
const rabbitMQService: RabbitMQService =
56+
globalAny.testApp.get(RabbitMQService);
57+
58+
if (rabbitMQService.connection) {
59+
await rabbitMQService.connection.close();
60+
console.log('RabbitMQ connection closed.');
61+
}
62+
} catch (error) {
63+
console.error('Error cleaning up RabbitMQ:', error);
64+
}
65+
}
66+
5067
beforeEach(() => {
5168
jest.resetAllMocks();
5269
});

0 commit comments

Comments
 (0)