Full answer
How it works
- Consumer side: tests run the consumer code against a mock of the provider. The mock records each interaction (request, expected response shape). The result is a contract file.
- The contract is shared (a broker such as Pact Broker, or a repository).
- Provider side: the provider's pipeline fetches the contracts of all its consumers, starts the provider (with test data for the given states), replays each request and checks the response against the matchers (types and structure, not exact values where unnecessary).
- Results are published. A "can I deploy" check blocks a release that would break a deployed consumer.
Compared with other tests
- End-to-end tests with everything deployed are slow, flaky and give late feedback. Contract tests run in each service's own pipeline.
- Provider unit and integration tests prove the provider works, but not that it still matches what its consumers expect.
- Schema checks (OpenAPI diff) find structural breaking changes in the whole API, while consumer contracts find only what matters to real consumers. They complement each other.
Practices: use type matchers, not exact values. Keep contracts small. Version contracts with the consumer. Cover errors and edge cases the consumer handles. Contracts also work for message queues (event payloads).
javascript
// A tiny contract test. A contract lists the requests a consumer makes and the shape it reads.
const contract = {
consumer: 'mobile-app', provider: 'student-service',
interactions: [
{ description: 'get a student', request: { method: 'GET', path: '/students/42' }, response: { status: 200, body: { id: 'number', name: 'string', city: 'string' } } },
{ description: 'unknown student', request: { method: 'GET', path: '/students/0' }, response: { status: 404, body: { error: 'string' } } },
],
};
function verify(provider, contract) {
const problems = [];
for (const { description, request, response } of contract.interactions) {
const actual = provider(request);
if (actual.status !== response.status) { problems.push(`${description}: status ${actual.status}, expected ${response.status}`); continue; }
for (const [field, type] of Object.entries(response.body)) {
if (!(field in actual.body)) problems.push(`${description}: missing field "${field}"`);
else if (typeof actual.body[field] !== type) problems.push(`${description}: "${field}" is a ${typeof actual.body[field]}, expected ${type}`);
}
}
return problems;
}
const providerV1 = ({ path }) => path === '/students/42' ? { status: 200, body: { id: 42, name: 'Asha', city: 'Pune', joined: '2026-01-01' } } : { status: 404, body: { error: 'not found' } };
const providerAdditive = (req) => { const r = providerV1(req); return r.status === 200 ? { ...r, body: { ...r.body, badges: ['early'] } } : r; };
const providerRenamed = (req) => { const r = providerV1(req); if (r.status !== 200) return r; const { name, ...rest } = r.body; return { ...r, body: { ...rest, fullName: name } }; };
const providerRetyped = (req) => { const r = providerV1(req); return r.status === 200 ? { ...r, body: { ...r.body, id: String(r.body.id) } } : r; };
for (const [label, provider] of [['current provider', providerV1], ['adds a field', providerAdditive], ['renames name to fullName', providerRenamed], ['id becomes a string', providerRetyped]]) {
const problems = verify(provider, contract);
console.log(label.padEnd(26), problems.length ? 'FAILS: ' + problems.join('; ') : 'passes');
}A simplified consumer contract and a verifier, in plain Node. Tools such as Pact add recording, a broker, provider states and many more matchers. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): current provider passes adds a field passes renames name to fullName FAILS: get a student: missing field "name" id becomes a string FAILS: get a student: "id" is a string, expected number
Key points
- Consumers state what they need; providers verify it.
- Failures appear in the provider's pipeline before release.
- Matchers check structure and types, not exact values.
- No need to deploy all services together.
- Complements schema diff checks and integration tests.
Common mistakes
- Contracts with exact values that break on every data change.
- Treating contract tests as a replacement for provider tests.
- Consumers that record everything, not only what they use.
- No 'can I deploy' check in the pipeline.
- Letting contracts go stale.
Follow-ups
- How do provider states work in Pact?
- How would you contract-test an event on a message queue?
- How does this relate to OpenAPI diff?
Tips
- Say 'consumer states needs, provider verifies'.
- Contrast with e2e tests.
- Mention type matchers.