The mock that lied
Green unit tests against a provider that no longer exists
The billing team renames amount to total and deploys. Your checkout's unit tests use a hand-written mock of the billing response. What do your tests do?
Your checkout calls a billing API. To keep its tests fast, it replaces the API with a mock: a response you typed by hand. The tests check the checkout against your mock. They say nothing about the real API, and nothing tells you when the two part ways.
When the provider renames a field, changes a type or starts returning a new status, your mock stays exactly as it was. The suite stays green and production breaks. A contract closes the gap. The consumer writes down what it reads, and the provider’s pipeline checks its real response against that list.
// The consumer's test owns the mock. Nothing ties it to the provider.
const mockBilling = { id: 'inv_101', amount: 48200, status: 'paid' };
test('shows the amount', () => {
const page = render(mockBilling);
expect(page.total).toBe('₦482.00'); // green forever
});// The consumer writes down what it relies on. The provider's pipeline
// verifies its real response against it before every deploy.
const contract = { id: 'string', amount: 'number', status: { enum: ['paid', 'pending'] } };
// consumer tests use a mock generated from the contract
// provider CI: verifyContract(contract, await realResponse('/invoices/inv_101')) Watch the invariant strip. In the first three scenarios the unit tests are green throughout. In FIX, the pipeline stops the rename, lets the added field through, and production keeps working.
The provider is about to deploy a change with no contract check in its pipeline. Insert one change before the deploy so the consumer's unit tests stay green and the production checkout breaks.
GOAL: unit tests green, production call fails
Write verifyContract(contract, response). The contract maps each field the consumer reads to 'string', 'number', 'boolean' or { enum: [...] }. Return an array of problem strings: empty when the response satisfies the contract. A field that is missing, has the wrong type, or holds a value outside its enum is a problem. Extra fields are fine: the provider may add what the consumer does not read. (A new status is caught here only because this contract declares the enum; a Pact-style contract covers just the examples the consumer recorded.)
A contract is not an integration test. It checks shape: field names, types, allowed values. It does not check that the provider’s numbers are right. It also only protects the fields the consumer lists, which is the point: the consumer can say “I read these three” and the provider stays free to change the rest. For how a retry changes what a client sees from an API, see idempotency.
Microservices with separate teams, mobile apps calling a backend, third-party APIs you wrap in a client. Tools such as Pact publish consumer contracts and verify them in the provider's CI. For a third-party API you cannot gate, run the same check on a schedule against the live response.
“Your unit tests pass but the service breaks in staging after another team's release. What happened and how do you stop it?” Name the hand-written mock, explain consumer-driven contracts verified in the provider's pipeline, say why extra fields must pass, and say what you still need integration tests for.
USED IN
Chapters that build on this lesson.
- Security testing: fuzzing, scanners and abuse casesSoftware Testing and Quality