This project demonstrates contract testing using Pact.js with a simple library API provider and consumer client.
Contract testing is a technique that helps ensure that services (such as an API provider and its consumers) can communicate with each other effectively. This project includes:
-
Consumer: The consumer is a service or application that uses the API provided by the provider. In our demo, it's represented by the
LibraryClientin theconsumerdirectory. The consumer defines its expectations of how the provider's API should behave. -
Provider: The provider is the service that offers the API. In our demo, it's the Express.js server in the
providerdirectory. The provider implements the API that the consumer uses and must meet the expectations set by the consumer.
- Node.js
- Express.js (for the provider API)
- Pact.js (for contract testing)
- Jest (for running tests)
-
Ensure you have Node.js installed (version 14 or higher recommended).
-
Clone this repository:
git clone git@github.com:nora-weisser/contract-testing-demo.git cd contract-testing-demo -
Install dependencies for both provider and consumer:
npm install
- To run consumer tests:
npm run test:consumer. If the test passes, a contract is generated and saved in thepactsfolder. If it fails, the contract cannot be created. - To run provider tests then:
npm run test:provider