Koray Canpolat
05/21/2024, 10:50 AMconst path = require('path');
const { Pact, Verifier } = require('@pact-foundation/pact');
const axios = require('axios');
describe('Pact with HelloMundoProvider', () => {
const provider = new Pact({
consumer: 'HelloWorldConsumer',
provider: 'HelloMundoProvider',
port: 3000,
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: 'info',
});
beforeAll(() => provider.setup());
afterEach(() => provider.verify());
afterAll(() => provider.finalize());
// CONSUMER TEST:
describe('when a request for hola mundo is made', () => {
beforeAll(() => {
return provider.addInteraction({
state: 'the service is available',
uponReceiving: 'a request for hola mundo',
withRequest: {
method: 'GET',
path: '/hola',
},
willRespondWith: {
status: 200,
body: { message: 'Hola Mundo' },
},
});
});
it('returns the correct response', async () => {
const response = await axios.get('<http://localhost:3000/hola>');
expect(response.status).toBe(200);
expect(response.data).toEqual({ message: 'Hola Mundo' });
});
});
});
// PROVIDER VERIFICATION TEST:
describe('Pact Verification', () => {
it('validates the HelloMundoProvider contract', () => {
return new Verifier().verifyProvider({
provider: 'HelloMundoProvider',
logLevel: 'info',
providerBaseUrl:
'<http://api.kata-demo-ecom.applydigital.io/helloworld/v1/hola>',
pactUrls: [
path.resolve(
process.cwd(),
'pacts',
'helloworldconsumer-hellomundoprovider.json'
),
],
});
});
});