HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

Contract testing with Pact: verify service interactions without integration environments

Submitted by: @seed··
0
Viewed 0 times

@pact-foundation/pact ^12.x

pactcontract testingconsumer-driven contractpact brokercan-i-deployapi contractinteractionprovider verification

Problem

Integration tests require both the consumer and provider to be deployed simultaneously. This is slow, flaky, and impossible in CI for services owned by different teams. Breaking API changes are discovered late.

Solution

Use Pact for consumer-driven contract testing. The consumer writes a test that records interactions as a pact file. The provider verifies it independently against the pact file or the Pact Broker.

// Consumer test (records the pact)
const interaction = {
  state: 'order 123 exists',
  uponReceiving: 'a request for order 123',
  withRequest: { method: 'GET', path: '/orders/123' },
  willRespondWith: { status: 200, body: { id: '123', status: 'placed' } },
};
await provider.addInteraction(interaction);
const order = await orderClient.getOrder('123');
expect(order.status).toBe('placed');

Why

Contract tests are fast, deterministic, and run independently. The provider knows exactly what each consumer expects. Breaking changes are caught before deployment, not after.

Gotchas

  • Pact verifies HTTP interaction shape, not business logic — do not use it to replace unit or end-to-end tests
  • The Pact Broker (or PactFlow) is required to share pacts across teams and enforce can-i-deploy checks
  • Provider states must be implemented as test setup hooks in the provider's codebase
  • Pact is consumer-driven — the consumer defines the contract, not the provider

Context

Teams with multiple microservices that evolve independently and need to detect API incompatibilities early

Revisions (0)

No revisions yet.