Contract testing APIs with Pact: consumer expectations, provider verification, and mock services with pact-js.
Agentic Workflow: Read -> Reason -> Act (contract-testing)
You are Contract Testing (api/general) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — api context for contract-testing
- Domain: Contract testing APIs with Pact: consumer expectations, provider verification, and mock services with pact-js.
- pact-consumer: Write consumer contract tests with pact-js against a mock provider —
npm install @pact-foundation/pact @pact-foundation/pact-node - pact-verify: Run provider verification against published pacts and mock the provider for local checks —
npx @pact-foundation/pact-cli verify --provider-base-url http://localhost:8080 - - Check
knowledgeandprerequisites: npm, npx
2. Reason — think for contract-testing
- For
pact-consumer: Write consumer contract tests with pact-js against a mock provider — decide which checks to run - For
pact-verify: Run provider verification against published pacts and mock the provider for local checks — decide which checks to run - Evaluate trust/policy:
kdesk trust+kdesk doctorpatterns for your inputs
3. Act — execute with contract-testing tools
- Tools:
Glob,Grep,Read,Bash,Npx(see frontmattertools/allowed-tools) - Use
safe_pathfor any write; record evidence (paths, checksums) - Fingerprint:
contract-testing:923c2a88
Contract Testing (Pact)
Test that consumers and providers agree on the API contract.
When to Use
- Proving API compatibility without full integration tests
- CI feedback on every consumer or provider change
- Replacing brittle end-to-end tests between teams
Consumer Test
npm install @pact-foundation/pact @pact-foundation/pact-node
const { PactV3 } = require('@pact-foundation/pact');
const provider = new PactV3({ consumer: 'WebApp', provider: 'UserService' });
describe('UserService contract', () => {
it('returns a user', async () => {
await provider.addInteraction({
uponReceiving: 'a request for a user',
withRequest: { method: 'GET', path: '/users/42' },
willRespondWith: { status: 200, body: { id: 42, name: 'alice' } },
});
await provider.executeTest(async (mockServer) => {
const res = await fetch(`${mockServer.url}/users/42`);
expect(await res.json()).toEqual({ id: 42, name: 'alice' });
});
});
});
npx jest --config jest.config.js
ls pacts/
Provider Verification
npx @pact-foundation/pact-cli verify \
--provider-base-url http://localhost:8080 \
--pact-urls ./pacts/*.json
Publish and Gate
npx @pact-foundation/pact-cli publish ./pacts --broker-base-url http://localhost:9292 --consumer-version 1.0.0
npx @pact-foundation/pact-cli can-i-deploy --pacticipant UserService --version 2.0.0 --to-environment production
Testing
# Break the provider response and re-run verification to see a mismatch
npm run test:provider
Best Practices
- Consumers define expectations; providers verify them
- Use provider states for data setup
- Run consumer tests fast in unit test suites
- Publish pacts from CI with the commit SHA as version
- Verify against latest pact of every consumer
- Gate deploys with can-i-deploy
- Keep matchers loose enough for safe evolution
Capabilities
pact-consumer
Write consumer contract tests with pact-js against a mock provider
Parameters:
consumer_name(string): Consumer application nameprovider_name(string): Provider application name
Commands:
npm install @pact-foundation/pact @pact-foundation/pact-nodenpx jest --config jest.config.jsnpm testnpm run test:contract
Examples:
- npm install @pact-foundation/pact && npx jest --config jest.config.js
- npm test -- --runInBand
- npx jest contracts/ -t 'a request for a user'
pact-verify
Run provider verification against published pacts and mock the provider for local checks
Parameters:
pact_urls(string): Glob or URL list of pact filesprovider_base_url(string): Base URL of the running provider
Commands:
npx @pact-foundation/pact-cli verify --provider-base-url http://localhost:8080 --pact-urls ./pacts/*.jsonnpx @pact-foundation/pact-cli publish ./pacts --broker-base-url http://localhost:9292 --consumer-version 1.0.0npx @pact-foundation/pact-cli can-i-deploy --pacticipant UserService --version 2.0.0 --to-environment productionnpm run test:provider
Examples:
- npx @pact-foundation/pact-cli verify --provider-base-url http://localhost:8080 --pact-urls ./pacts/*.json
- npx @pact-foundation/pact-cli can-i-deploy --pacticipant WebApp --version 1.0.0 --to-environment production
- npx @pact-foundation/pact-cli publish ./pacts --broker-base-url http://localhost:9292 --consumer-version 1.0.0