diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 2b29066..8cac004 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -1,61 +1,15 @@ -import { - AD_DIRECTION_ENCODER, - AD_MESSAGE_PUBLISHER, - AD_REPOSITORY, -} from '@modules/ad/ad.di-tokens'; -import { AdMapper } from '@modules/ad/ad.mapper'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; import { PrismaService } from '@modules/ad/infrastructure/prisma.service'; -import { PostgresDirectionEncoder } from '@modules/geography/infrastructure/postgres-direction-encoder'; -import { ConfigModule } from '@nestjs/config'; -import { EventEmitterModule } from '@nestjs/event-emitter'; -import { Test } from '@nestjs/testing'; +import { integrationTestingModule } from './integration.setup'; describe('Ad Repository', () => { let prismaService: PrismaService; let adRepository: AdRepository; - const mockMessagePublisher = { - publish: jest.fn().mockImplementation(), - }; - - const mockLogger = { - log: jest.fn(), - warn: jest.fn(), - error: jest.fn(), - }; - beforeAll(async () => { - const module = await Test.createTestingModule({ - imports: [ - EventEmitterModule.forRoot(), - ConfigModule.forRoot({ isGlobal: true }), - ], - providers: [ - PrismaService, - AdMapper, - { - provide: AD_REPOSITORY, - useClass: AdRepository, - }, - { - provide: AD_MESSAGE_PUBLISHER, - useValue: mockMessagePublisher, - }, - { - provide: AD_DIRECTION_ENCODER, - useClass: PostgresDirectionEncoder, - }, - ], - }) - // disable logging - .setLogger(mockLogger) - .compile(); - - prismaService = module.get(PrismaService); - adRepository = module.get(AD_REPOSITORY); + ({ prismaService, adRepository } = await integrationTestingModule()); }); afterAll(async () => { diff --git a/src/modules/ad/tests/integration/integration.setup.ts b/src/modules/ad/tests/integration/integration.setup.ts new file mode 100644 index 0000000..d1cb231 --- /dev/null +++ b/src/modules/ad/tests/integration/integration.setup.ts @@ -0,0 +1,57 @@ +import { + AD_DIRECTION_ENCODER, + AD_MESSAGE_PUBLISHER, + AD_REPOSITORY, +} from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; +import { PrismaService } from '@modules/ad/infrastructure/prisma.service'; +import { PostgresDirectionEncoder } from '@modules/geography/infrastructure/postgres-direction-encoder'; +import { ConfigModule } from '@nestjs/config'; +import { EventEmitterModule } from '@nestjs/event-emitter'; +import { Test } from '@nestjs/testing'; + +export async function integrationTestingModule(): Promise<{ + prismaService: PrismaService; + adRepository: AdRepository; +}> { + const mockMessagePublisher = { + publish: jest.fn().mockImplementation(), + }; + + const mockLogger = { + log: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; + + const module = await Test.createTestingModule({ + imports: [ + EventEmitterModule.forRoot(), + ConfigModule.forRoot({ isGlobal: true }), + ], + providers: [ + PrismaService, + AdMapper, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, + { + provide: AD_MESSAGE_PUBLISHER, + useValue: mockMessagePublisher, + }, + { + provide: AD_DIRECTION_ENCODER, + useClass: PostgresDirectionEncoder, + }, + ], + }) + .setLogger(mockLogger) + .compile(); + + return { + prismaService: module.get(PrismaService), + adRepository: module.get(AD_REPOSITORY), + }; +}