A Consumer-Driven Contract Testing Demo
This is a simple setup to demonstrate how services can interact with a minimal expectation of breaking in production due to inevitable changes in a service which is being depended upon
This is done using the pact library
For simplicity, the project has the following services:
- client (consumer)
- api (provider)
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes
- Check the settings in the
docker-compose.yml
file and rundocker-compose up -d
in root folder
This is to get a working mysql server running and also to start up the pact broker
- Make sure to create a local database for the pacts to be stored in and reconfigure the environment for the pact broker in the
docker-compose.yml
file if need be
Similar to the workflow of your usual consumer-driven contract tested application, we follow these steps
- First run the consumer test,
npm run test:consumer
With this action, the necessary pacts are created and stored in a newly created pacts
folder
- For local testing purposes, publish the generated pacts if everything checks out by running
npm run publish-pacts
.
NB: Unfortunately this is a Linux specific command in relation to sh
and will be addressed soon.
- Check the broker base url (localhost:9092) and see the pacts successfully published in a simple ui
- Now we focus on our movie api which serves as our provider. We run
npm run test:provider
. This, if it passes, verifies the pact/contract and this can also be confirmed in the broker ui served on localhost:9092
- To better appreciate this, tweak the response from the api and see as the provider test fails due to difference in expectation between what the contract expects and what the api is returning