Skip to content

Instantly share code, notes, and snippets.

@venkata-qa
Last active October 13, 2025 06:25
Show Gist options
  • Select an option

  • Save venkata-qa/6cc5a268fa1eed88d07e632f8a14663a to your computer and use it in GitHub Desktop.

Select an option

Save venkata-qa/6cc5a268fa1eed88d07e632f8a14663a to your computer and use it in GitHub Desktop.
import path from 'path';
import { PactV3, MatchersV3, SpecificationVersion } from '@pact-foundation/pact';
import {
getServiceCategories,
getAppointmentsFormat,
getPractitionerGender,
getAppointmentLocations,
getSlots,
getAppointments,
getAppointment,
} from '../__tests__/client/booking.endpoint';
// Update the file path if bookingAxiosInstance is available elsewhere:
import axios from 'axios';
// Helper: try to import your actual axios instance, else use a new instance and inject it
let bookingAxiosInstance;
try {
// @ts-ignore
bookingAxiosInstance = require('../__tests__/client/api/axiosInstance').bookingAxiosInstance;
} catch {
bookingAxiosInstance = axios.create();
}
const { eachLike, like } = MatchersV3;
const BASE_HEADERS = {
Authorization: 'Bearer token123',
Accept: 'application/json',
'Content-Type': 'application/json',
};
const provider = new PactV3({
consumer: 'BookingFrontend',
provider: 'BookingService',
dir: path.resolve(__dirname, 'pacts'),
logLevel: 'info',
spec: SpecificationVersion.SPECIFICATION_VERSION_V3,
host: '127.0.0.1',
port: 0,
});
describe('Booking Contract - Real Client Approach', () => {
beforeEach(() => {
// Just to be sure, overwrite baseURL before each test (isolation)
bookingAxiosInstance.defaults.baseURL = undefined;
});
it('should get service categories', async () => {
provider.addInteraction({
states: [{ description: 'service categories exist' }],
uponReceiving: 'a request for service categories',
withRequest: {
method: 'GET',
path: '/api/v1/appointments/services/categories',
headers: BASE_HEADERS,
},
willRespondWith: {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: like({
data: eachLike({
id: like('cat-123'),
label: like({ key: 'key', value: 'category' }),
options: eachLike({ id: like('option-1'), label: like({ key: 'o', value: 'option label' }) }),
}),
}),
},
});
await provider.executeTest(async (mockServer) => {
bookingAxiosInstance.defaults.baseURL = mockServer.url;
const response = await getServiceCategories()();
expect(response.status).toBe(200);
expect(response.data).toBeDefined();
});
});
it('should get appointment formats', async () => {
provider.addInteraction({
states: [{ description: 'appointment formats exist' }],
uponReceiving: 'a request for appointment formats',
withRequest: {
method: 'GET',
path: '/api/v1/appointments/formats',
headers: BASE_HEADERS,
},
willRespondWith: {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: like({
data: eachLike({
id: like('format-123'),
label: like({ key: 'k1', value: 'format value' }),
options: eachLike({ id: like('fo1'), label: like({ key: 'op', value: 'option format' }) }),
}),
}),
},
});
await provider.executeTest(async (mockServer) => {
bookingAxiosInstance.defaults.baseURL = mockServer.url;
const response = await getAppointmentsFormat({})();
expect(response.status).toBe(200);
expect(response.data).toBeDefined();
});
});
it('should get practitioner genders', async () => {
provider.addInteraction({
states: [{ description: 'practitioner genders exist' }],
uponReceiving: 'a request for practitioner genders',
withRequest: {
method: 'GET',
path: '/api/v1/appointments/practitioners/genders',
headers: BASE_HEADERS,
},
willRespondWith: {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: like({
data: eachLike({
id: like('gender-1'),
label: like({ key: 'gk', value: 'gender label' }),
options: eachLike({ id: like('go1'), label: like({ key: 'gop', value: 'option' }) }),
}),
}),
},
});
await provider.executeTest(async (mockServer) => {
bookingAxiosInstance.defaults.baseURL = mockServer.url;
const response = await getPractitionerGender({})();
expect(response.status).toBe(200);
expect(response.data).toBeDefined();
});
});
// Add more tests for locations, slots, appointments, etc., following the same pattern.
// Example:
// - getAppointmentLocations(params)()
// - getSlots(params)()
// - getAppointments(params)()
// - getAppointment(appointmentId)()
// Adjust the test as you add more endpoints!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment