Consumer-driven contracts and breaking changes
In consumer-driven contracts (Pact) the consumer describes the requests it makes and the response fields it uses, and the provider replays them in its CI. Removing or renaming a used field, changing a type, a new required request field, or changing an error code or format is a breaking change that fails before deploy. A new enum value can also break a client with no default branch.
API · Contracts
The back end renamed a field and the front end learned about it from production. How to catch it before deploy?
The provider team changed amount to sum because it looked "cleaner". Their tests are green, they wanted it. The front end, which reads amount, shows "NaN" in production. Two teams, each with its own tests, and nobody watches that they agree. Contract testing exists for that.
The lesson covers consumer-driven contracts (Pact), what breaks clients and what does not, and why a new enum value is not always safe.
How it works and why
Integration tests between two services are slow and brittle: both must run, plus all their dependencies. Contract testing splits the check into two parts, each run in isolation. In the consumer-driven approach (Pact) the consumer produces the contract: its test describes which requests the client makes and which response fields it actually uses. The requests go to a mock provider, and Pact records what happened in a pact file (JSON).
The file is then published to a Pact Broker (or PactFlow), and the provider in its own CI replays the recorded requests against the real service and checks that the responses fit. The result goes back to the broker, and a team can ask can-i-deploy: is this version compatible with all consumers in the target environment.
The Pact flow
The key idea: the contract contains only what the consumer really uses. So the provider can change everything else without fear of breaking someone. And the other way round: when it removes a field someone uses, its CI fails before deploy, naming the consumer and the exact request.
Example: a consumer test and a breaking change
A consumer test (Pact JS, V3) describes one interaction and at the same time checks the client code:
import { PactV3, MatchersV3 } from '@pact-foundation/pact';
const { like, regex } = MatchersV3;
const pact = new PactV3({ consumer: 'web-app', provider: 'transfers-api' });
test('web-app reads a transfer', async () => {
pact
.given('transfer tr_91 exists')
.uponReceiving('a request for transfer tr_91')
.withRequest({ method: 'GET', path: '/v1/transfers/tr_91' })
.willRespondWith({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: {
id: like('tr_91'),
status: regex('pending|completed|failed', 'pending'),
amount: like(150),
},
});
await pact.executeTest(async (mockServer) => {
const t = await getTransfer(mockServer.url, 'tr_91'); // the real client code
expect(t.amount).toBe(150);
});
});
Here like says "the type matters, not the exact value", and regex sets allowed values with an example. The given block is the "provider state": before replaying the request the provider must prepare the transfer tr_91 (on its side this is a state handler). Thanks to this the pact file does not depend on database contents.
What counts as a breaking change. The yardstick: will a consumer written against the previous contract break.
| Change | Type |
|---|---|
| Add a new optional response field | Non-breaking |
| Add a new endpoint | Non-breaking |
| Add an optional request parameter | Non-breaking |
| Remove or rename a response field | Breaking |
| Change a field type, for example number to string | Breaking |
| Make an optional request field required | Breaking |
| Change a status code or the error format | Breaking |
A nuance: a new enum value is formally an addition but can break a client that knows only the old values:
before: status: { type: string, enum: [pending, completed, failed] }
after: status: { type: string, enum: [pending, completed, failed, reversed] }
client: switch (t.status) { case 'pending': ... case 'completed': ... case 'failed': ... }
// "reversed" falls into default: spinner forever
So for an enum the contract records what the consumer can handle, and in practice teams agree on a "tolerant reader": the client must react safely to an unknown value instead of crashing.
Limits and alternatives
- Pact does not replace the provider's functional tests. It checks that the response has the shape a consumer expects, not that the service calculates correctly.
- A consumer must exist and write contracts. For a public API with unknown clients consumer-driven does not fit; a specification, version comparison (oasdiff) and API versioning work there.
- Bi-directional contracts in PactFlow match the consumer pact against the provider's specification, so the provider does not have to replay requests. It is cheaper but checks only what could be compared with the schema.
- Versioning is still needed: a breaking change without a new API version (
/v2, a separate header or a compatibility period) breaks those the contracts did not cover.
Typical mistakes
- Describing the whole response in the pact instead of the fields the consumer needs. The contract becomes brittle and the provider cannot change anything without a red build.
- Hard values instead of matchers. The test fails on a different id or date, not on a real violation.
- Not publishing provider verification results. Without them
can-i-deployhas no data. - Treating an enum as safe to extend. A new value breaks a consumer with no default branch.
Check yourself
The provider removed a field no consumer uses. Will Pact fail?
Answer
No: the pact file holds only what consumers actually read. That is both the advantage (the provider is free to change) and the limit: a field read by an unknown client is not protected by contracts.
Why can adding a value to a response enum be a breaking change although it is "only an addition"?
Answer
A client that handles a list of values with no default branch receives an unknown value and breaks or shows an empty state. So a consumer must handle unknown values safely and a provider must announce enum extensions.
can-i-deploy gives the release go-ahead. The contract holds only the fields the consumer needs and matchers, not exact values. It complements functional tests and versioning rather than replacing them, and for public APIs with unknown consumers I work from the specification and version comparison.