Intermediate LevelREST APIs & Web Services
What is Consumer-Driven Contract Testing (Pact) for REST microservices?
❓ Interview Prompt: Explain Consumer-Driven Contract Testing, Pact framework, verifying API contracts without running full microservice clusters, and CI/CD pipeline integration.
Consumer-Driven Contract Testing (Pact) verifies that microservices can communicate without requiring expensive, brittle end-to-end integration test environments:
How Pact Works:
1
Consumer Defines Contract: The frontend / API consumer defines expected HTTP requests and responses in a Pact file.
2
Provider Verifies Contract: The backend provider runs the Pact contract file against its actual endpoints in CI/CD, guaranteeing backend edits never break consumer assumptions!
Code Implementation Example
typescript
import { PactV3, MatchersV3 } from '@pact-foundation/pact';
const provider = new PactV3({ consumer: 'FrontendApp', provider: 'UserService' });
provider.addInteraction({
states: [{ description: 'User 42 exists' }],
uponReceiving: 'A request for user 42',
withRequest: { method: 'GET', path: '/api/v1/users/42' },
willRespondWith: {
status: 200,
body: { id: '42', name: MatchersV3.string('Alice') }
}
});💡Defining consumer contract expectations using Pact framework in TypeScript.
🎯 Interview Key Takeaway
Pact Consumer-Driven Contract Testing verifies frontend and backend API expectations in CI/CD without running full microservice clusters.
#Pact#Contract Testing#Microservices#Intermediate
Back to all REST APIs & Web Services Questions→