Created
September 6, 2023 19:09
-
-
Save acastro969/6342a023d9b2e5f64da7249922379728 to your computer and use it in GitHub Desktop.
PactProviderVerification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Test } from '@nestjs/testing'; | |
import { INestApplication } from '@nestjs/common'; | |
import { PactProviderModule, PactVerifierService } from 'nestjs-pact'; | |
import { WeatherData } from '../../src/products/entities/product.entity'; | |
import { Repository } from 'typeorm'; | |
import { AppModule } from '../../src/app.module'; | |
import { ConfigModule } from '@nestjs/config'; | |
import { getRepositoryToken } from '@nestjs/typeorm'; | |
describe('Verificación de Cambio de Proveedor Pact', () => { | |
let verifier: PactVerifierService; | |
let app: INestApplication; | |
const weatherData = new WeatherData(); | |
weatherData.location = 'Buenos Aires'; | |
weatherData.temperature = '15'; | |
weatherData.condition = 'Sunny'; | |
beforeAll(async () => { | |
// Crea una referencia de módulo para las pruebas | |
const moduleRef = await Test.createTestingModule({ | |
imports: [ | |
ConfigModule.forRoot({ | |
isGlobal: true, | |
}), | |
AppModule, | |
PactProviderModule.register({ | |
// Configura PactProviderModule | |
provider: 'WeatherDataService', // Nombre del proveedor | |
logLevel: 'debug', // Nivel de registro | |
stateHandlers: { | |
'Weather data exists': async () => { | |
// Setea un estado para el proveedor | |
app = moduleRef.createNestApplication(); | |
// Inicializa la aplicación NestJS | |
await app.init(); | |
// Accede al repositorio WeatherData | |
const weatherDataRepository: Repository<WeatherData> = | |
moduleRef.get<Repository<WeatherData>>( | |
getRepositoryToken(WeatherData), | |
); | |
// Guarda datos climáticos simulados | |
await weatherDataRepository.save(weatherData, { reload: false }); | |
}, | |
}, | |
pactBrokerUrl: process.env.PACT_BROKER_URL, // URL del Pact Broker | |
consumerVersionSelectors: [ | |
{ mainBranch: true }, // Selectores de versión del consumidor | |
{ deployed: true }, | |
{ matchingBranch: true }, | |
], | |
publishVerificationResult: true, // Publicar resultado de verificación | |
providerVersionBranch: process.env.GIT_BRANCH, // Rama de la versión del proveedor | |
enablePending: true, // Habilitar pacts pendientes | |
providerVersion: process.env.SHORT_SHA, // Versión del proveedor | |
failIfNoPactsFound: false, // Fallar si no se encuentran pacts | |
}), | |
], | |
}).compile(); | |
// Obtener la instancia de PactVerifierService | |
verifier = moduleRef.get(PactVerifierService); | |
// Crear una instancia de la aplicación NestJS | |
app = moduleRef.createNestApplication(); | |
// Inicializar la aplicación NestJS | |
await app.init(); | |
}, 60000); | |
it('valida las expectativas del Servicio Coincidente', async () => { | |
// Verificar las expectativas del Servicio Coincidente | |
await verifier.verify(app); | |
}, 60000); | |
afterAll(async () => { | |
// Cerrar la aplicación NestJS | |
await app.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment