An example of writing contract test with
pact
andjest-pact
.The app itself is not important, our goal is too illustrated how straightforward it is to write a contract test.
Having an integration test that check on your HTTP client. with mock (
nock
,msw
) is really close
We aim to check we are able to do a
GET
request on/api/customers
and/api/customer/:id/orders
and get the correct data shape.
Test file | Description | Benefits |
---|---|---|
index |
an integration test with a mock server (msw) | - check HTTP client's plumbing |
index-matcher-v2.contract |
a contract test using pact specs@v2 and matchers@v2 | - check HTTP client's plumbing - generate contract |
index-matcher-v3.contract |
a contract test using pact specs@v3 and matchers@v3 | - check HTTP client's plumbing - generate contract |
❯ yarn install
❯ yarn test
❯ yarn test:contracts # both contract test files
❯ jest --testMatch '**/*v2.contract.test.ts'
❯ jest --testMatch '**/*v3.contract.test.ts'
requirements: [make].
To publish you need to download the Pact-CLI binary:
❯ make install-pact-cli
Then edit the .env
to fill:
PACT_BROKER_URL=https://your.broker.org/
Finally, connect to VPN and publish:
❯ make publish-contract
Follow the How to Add repo URL and main branch metadata.