# Contract Testing

> Contract testing APIs with Pact: consumer expectations, provider verification, and mock services with pact-js. Use when working with pact consumer, pact verify, api or when the user mentions pact consumer, pact verify, api.

- Skill: `opknock/contract-testing` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/contract-testing`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/contract-testing/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/contract-testing

---


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 `knowledge` and `prerequisites: 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 doctor` patterns for your inputs

### 3. Act — execute with `contract-testing` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash`, `Npx` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for 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

```bash
npm install @pact-foundation/pact @pact-foundation/pact-node
```

```js
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' });
    });
  });
});
```

```bash
npx jest --config jest.config.js
ls pacts/
```

## Provider Verification

```bash
npx @pact-foundation/pact-cli verify \
  --provider-base-url http://localhost:8080 \
  --pact-urls ./pacts/*.json
```

## Publish and Gate

```bash
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

```bash
# 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 name
- `provider_name` (string): Provider application name

**Commands:**
- `npm install @pact-foundation/pact @pact-foundation/pact-node`
- `npx jest --config jest.config.js`
- `npm test`
- `npm 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 files
- `provider_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/*.json`
- `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`
- `npm 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

## References
- [Pact Docs](https://docs.pact.io/)
- [Pact JS GitHub](https://github.com/pact-foundation/pact-js)

