contract-testing-with-pact
Implementing consumer-driven contract testing for microservices using Pact.
Installation
Follow the repository instructions for this skill and your agent. Review the files and dependencies before adding it to your workflow.
View source ↗GuidesSkill instructions
Contract Testing with Pact
Overview
Contract testing validates that service consumers and providers agree on request/response expectations. Pact implements consumer-driven contracts (CDC) with shareable pact files and provider verification.
Table of Contents
- What is Contract Testing
- Consumer-Driven Contracts
- Pact Fundamentals
- Pact vs Integration Testing
- Writing Consumer Tests
- Provider Verification
- Pact Broker
- CI/CD Integration
- Bi-Directional Contracts
- Async Messaging
- GraphQL
- Language Support
- Patterns and Anti-Patterns
- Breaking Change Strategy
- Monitoring Compliance
What is Contract Testing
Contract tests validate API interactions without full end-to-end setups. They prevent breaking changes by verifying expectations explicitly.
Consumer-Driven Contracts
Consumers define expectations; providers must satisfy them:
- Faster feedback for consumers
- Clear API expectations
- Reduced integration surprises
Pact Fundamentals
- Consumer tests: Generate pact files.
- Provider verification: Validate pact files.
- Pact files: JSON contracts.
- Pact Broker: Store and manage contracts.
Pact vs Integration Testing
- Pact: Validates interface compatibility.
- Integration: Validates full system behavior.
Use both for comprehensive coverage.
Writing Consumer Tests
Define interactions with matchers:
- Exact value
- Type-based matcher
- Regex matcher
Example (TypeScript):
await provider.addInteraction({
state: 'user exists',
uponReceiving: 'get user',
withRequest: { method: 'GET', path: '/users/123' },
willRespondWith: {
status: 200,
body: { id: like(123), name: like('Alice') }
}
});
Provider Verification
Provider verifies against published contracts:
- Implement state handlers
- Validate response headers/body/status
- Support pending and WIP pacts for safe changes
Pact Broker
Key features:
- Publish and version contracts
- Webhooks on new contracts
- Can-I-Deploy checks for promotion
- Visibility into compatibility
CI/CD Integration
Recommended flow:
- Consumer tests generate pact
- Publish pact to broker
- Provider verifies in CI
- Use Can-I-Deploy before release
Bi-Directional Contracts
Support for provider-defined expectations plus consumer constraints:
- Useful for GraphQL or schema-first APIs
- Prevents provider drift
Async Messaging
Pact supports message-based contracts:
- Define message payload expectations
- Verify consumer and provider handlers
GraphQL
Contract test schema and query responses:
- Use schema as a contract baseline
- Validate query response shapes
Language Support
SDKs available for:
- JavaScript/TypeScript
- Python
- Java
- Go
Patterns and Anti-Patterns
Good:
- Use flexible matchers
- Keep interactions focused
Avoid:
- Over-specifying exact values
- One contract covering multiple unrelated cases
Breaking Change Strategy
- Use pending pacts for backward-compatible changes.
- Version APIs when breaking changes are required.
- Maintain old contract until consumers migrate.
Monitoring Compliance
Track contract verification in CI and alert on failed verification runs.
Related Skills
-
16-testing/integration-testing -
09-microservices/service-design -
03-backend-api/express-rest
Examples
Read the usage examples in the source repository.
View source ↗Source history
- Indexed on
- 2026-01-20
- Index updated
- 2026-01-26
- Branch
- main
Index and sync dates describe this directory. View the source repository for code history.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!