This is a sample project to demonstrate consumer driven contract testing using the PACT framework and TypeScript.
This repo consists of a provider service
and a consumer service
. The interactions between these 2 services will be tested used PACT.
The provider
service which is responsible for CRUD-ing & maintaining a movie database and the consumer
service makes use of the APIs exposed by the provider
to do further operations.
├── README.md
├── consumer # consumer service
│ ├── jest.config.js # Jest test configurations
│ ├── logs # Logs generated by pact for debugging issues
│ │ └── pact.log
│ ├── package.json
│ ├── src
│ │ ├── actions.ts # Contains application business logic
│ └── └── pacts
└── movies.contract.test.ts # Contains the consumer contract tests
├── contracts
│ └── movie_consumer-movie_producer.json # Generated PACT contract
└── provider # provider service
├── jest.config.js # Jest test configurations
├── package.json
├── pacts
│ └── movies.contract.test.ts # Contains the provider contract tests
├── src
│ ├── actions.ts # Contains application business logic
│ ├── app.ts # Contains the API endpoints
│ ├── data # Sqlite Datastore of the service
│ ├── entity # TypeORM entities
└── └── server.ts # Server initializer
This project is written with Node version v12.18.4
Install node
https://nodejs.org/en/
Install yarn
https://classic.yarnpkg.com/en/docs/install#mac-stable
Go to the provider directory
cd provider/
Run yarn to get all the required libraries
yarn
To run the provider server run
yarn start
This brings up the application at http://localhost:3001
You can hit http://localhost:3001/movies
to return a list of movies in the system
Go to the consumer directory
cd consumer/
Run yarn to get all the required libraries
yarn
The tests require a PACT broker to be running. To start the broker run the below command from the root project directory
docker-compose up
This brings up the broker application as well a PostgreSQL database to store the contracts and verification results.
The broker app can be accessed at http://localhost:9292/
Go to the consumer directory
cd consumer/
Run
yarn test
The above command runs the contract tests and generates the PACT contract file under the contracts
directory
Go to the provider directory
cd provider/
Before running the provider test, you need to bring up the provider server. You can do that by running
yarn start
Once the application is up run the tests using
yarn test
The above command picks up the PACT contract file from the contracts
directory which was generated by the consumer contract tests and run the tests against the provider.
The docker pact-cli can be used to perform multiple functions like running the verifications, accessing the broker and also to verify whether the application can be deployed safely.
A sample command to run can-i-deploy
docker run --rm pactfoundation/pact-cli broker can-i-deploy --pacticipant "Movie Consumer" --latest --broker-base-url http://host.docker.internal:9292
https://hub.docker.com/r/pactfoundation/pact-cli https://hub.docker.com/r/pactfoundation/pact-broker https://github.com/pact-foundation/pact-js