Skip to content

This is a sample project to demonstrate consumer driven contract testing using the PACT framework and TypeScript.

License

Notifications You must be signed in to change notification settings

amalsgit/typescript_pact_contract_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consumer Driven Contract Tests with Pact & TypeScript

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.

Project Structure

├── 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

Installation

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

Provider SetUp

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

Consumer SetUp

Go to the consumer directory

cd consumer/

Run yarn to get all the required libraries

yarn

PACT Broker Setup

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/

Running Consumer Contract Tests

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

Running Provider Contract Tests

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.

pact-cli

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

References

https://hub.docker.com/r/pactfoundation/pact-cli https://hub.docker.com/r/pactfoundation/pact-broker https://github.com/pact-foundation/pact-js

About

This is a sample project to demonstrate consumer driven contract testing using the PACT framework and TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published