Pact end to end: consumer test, Broker, can-i-deploy
The Pact cycle: a consumer test against a mock provider, publishing the pact to the Broker, provider verification on real code, and a check before deploy. It is can-i-deploy that turns Pact into release protection: without it a red verification blocks nothing. The version is a git SHA, the branch is passed separately, and the environment is recorded with record-deployment. Provider states must be independent.
API · Contract testing in practice
The pact files exist, yet nothing blocks a deploy. Which step of the cycle is missing?
A team set up Pact: the consumer generates contracts, the provider verifies them, the Broker stores the results. A month later the provider ships a release that breaks the consumer and still reaches production: the verification was red, but nobody asked the Broker before the deploy. The lesson goes through the whole cycle and shows which step turns Pact from "extra tests" into protection of a release.
How it works and why it is designed so
- The consumer test. The client code (not the UI!) calls a mock provider that Pact starts locally. The test describes an interaction: a state (
given), a request and the expected response. After the test passes, a pact file (JSON) is generated. - Publishing. The consumer's CI publishes the pact to the Pact Broker (or PactFlow) with a version, usually the git SHA, and a branch.
- Provider verification. The provider runs a
Verifierthat takes pacts from the Broker, replays every request against a really running provider (with its dependencies stubbed) and compares the response. The result is published back with the provider version. - The deploy decision. Before a release either side asks the Broker whether this version is verified against what is currently in that environment. After the deploy the fact is recorded.
The Pact cycle from a test to a release
Worked example: what to add to the pipeline
Three commands close the cycle. The first publishes the consumer's contracts, the second asks the Broker before a release, the third records that the version is now in production:
# 1. the consumer publishes the pact
pact-broker publish ./pacts --consumer-app-version $GIT_SHA --branch $BRANCH
# 2. before a release: is this version verified against what is in production
pact-broker can-i-deploy --pacticipant web-app --version $GIT_SHA --to-environment production
# 3. after the deploy: record the fact
pact-broker record-deployment --pacticipant web-app --version $GIT_SHA --environment production
can-i-deploy looks in the matrix "consumer version × the provider version currently in this environment" for a successful verification. If there is none, the command returns a non-zero code and the deploy step does not start. If this line is missing from the pipeline, a field renamed by the provider reaches production even though the verification was red.
On the provider side the verification is set up like this:
import { Verifier } from '@pact-foundation/pact';
await new Verifier({
provider: 'orders-api',
providerBaseUrl: 'http://localhost:8080',
pactBrokerUrl: process.env.PACT_BROKER_URL,
consumerVersionSelectors: [
{ mainBranch: true },
{ matchingBranch: true },
{ deployedOrReleased: true },
],
enablePending: true,
providerVersion: process.env.GIT_SHA,
providerVersionBranch: process.env.BRANCH,
publishVerificationResult: process.env.CI === 'true',
stateHandlers: {
'order 42 exists': async () => seedOrder(42),
},
}).verifyProvider();
Details that break the cycle
- Provider states. An interaction says "when order 42 exists, GET /orders/42 returns…". During verification the provider must bring the system into that state through
stateHandlers: create test data or set up stubs. The states should be independent and repeatable. - Versions, branches, environments. The modern model: a pacticipant version = git SHA, the branch is passed separately (
--branch), and "what is in production now" is tracked by environments (record-deployment, for libraries and mobile appsrecord-release). Tags, which used to play this role, are no longer recommended by the Pact documentation for new setups. - Selectors. The verification chooses pacts through
consumerVersionSelectors: usuallymainBranch,matchingBranchanddeployedOrReleased. WithoutdeployedOrReleasedthe provider does not check what really runs in production. - Pending pacts and WIP. A new pact from a consumer branch that the provider does not yet satisfy should not break the provider's build: with
enablePending: trueits verification does not block until it has passed at least once.includeWipPactsSincepulls in such "work in progress" pacts automatically. - Webhooks. So as not to wait for the provider's next build, the Broker has a webhook for the
contract_requiring_verification_publishedevent: a new or changed agreement triggers verification in the provider's CI, and the result returns to the Broker.
Common mistakes
- No
can-i-deploybefore a deploy: the contracts exist, the protection does not. - Not calling
record-deployment: the Broker does not know what is in production, and the matrix compares against the wrong version. - Writing the consumer test through the UI instead of the client layer: the test is slow and brittle.
- States that depend on data from another test: the verification is unstable.
Check yourself
The provider verification is red but the release still went through. What exactly is missing in the pipeline?
Answer
The can-i-deploy step before the deploy (and its non-zero exit code that stops the pipeline). Without it the verification result stays information in the Broker, not a release gate.
Why does the provider need enablePending when a consumer's new pact is not yet satisfied?
Answer
So that a new, not yet supported pact from a consumer branch does not break the provider's build until it has passed at least once. Otherwise any new consumer requirement would stop another team.