- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Pact Foundations
In this Code Lab, you will learn the fundamentals of consumer-driven contract testing with Pact. You'll build and test a Product Client (Consumer) and a Product API (Provider), ensuring they can communicate reliably without flaky end-to-end tests.
Lab Info
Table of Contents
-
Challenge
Step 1: Introduction and Setup
Welcome to this Code Lab on Pact Foundations! Today, you'll learn the principles of consumer-driven contract testing, a powerful technique for ensuring microservices can communicate reliably without slow and brittle end-to-end integration tests.
In this scenario, there are two services:
ProductClient(The Consumer): A client application that needs to fetch product dataProductAPI(The Provider): An API that serves product data
Your goal is to create a contract that guarantees
ProductClientandProductAPIare compatible. You'll write the consumer's expectations first, generate a contract file (the "pact"), and then verify that your provider implementation fulfills that contract.Getting Started
The project consists of two main applications:
├── consumer/ # ProductClient (Consumer application) │ ├── src/ │ │ └── client.ts │ └── test/ │ └── consumer.test.ts ├── provider/ # ProductAPI (Provider application) │ ├── src/ │ │ ├── server.ts │ │ ├── controller.ts │ │ └── data.ts │ └── test/ │ └── provider.test.ts ├── pacts/ # Generated contract files └── runTest.sh # Test execution scriptRunning the Applications
Consumer Application
The consumer is a client library that makes HTTP requests to the provider API.
Run Consumer Tests:
cd consumer npm testThis will:
- Execute the Pact consumer tests
- Generate a contract file in
pacts/productclient-productapi.json
Provider Application
The provider is an Express.js API server that serves product data.
Start the Provider Server:
cd provider npm startThe server will start on
http://localhost:8081Run Provider Verification Tests:
cd provider npm testThis will:
- Start the provider server
- Verify the provider against the consumer contract
- Ensure all interactions defined in the contract are satisfied
It's time to get started! > This lab experience was developed by the Pluralsight team using Forge an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 2: Build the API Consumer Client
In consumer-driven contract testing, you always start with the consumer. The consumer's needs drive the API design. Your first step is to build the client-side logic that will eventually communicate with the Product API. You'll implement two simple functions using
axiosto handle the HTTP requests. -
Challenge
Step 3: Define and Generate the Consumer Contract
Now that your client is built, you need to formally define its expectations of the provider. You'll write a Pact test that does three things:
- Defines the
interactions(expected request/response pairs) your client will make - Starts a mock server that will respond exactly as defined in the interactions
- Runs your client code against the mock server
If the client code makes the requests it's supposed to, the test passes, and Pact generates a
pact.jsonfile. This file is the contract. - Defines the
-
Challenge
Step 4: Implement the Provider API
With the contract generated, you'll now switch hats and become the provider developer. Your job is to build an API that satisfies all the expectations laid out in the
pact.jsonfile. You'll use Express.js to create a simple server that serves product data from an in-memory store. -
Challenge
Step 5: Verify the Provider Against the Contract
This is the final and most crucial step. You have a contract from the consumer and a real API implementation from the provider. Now, you need to verify they are compatible.
Pact's Verifier will:
- Read the
pact.jsonfile. - Start your real Provider API server.
- Replay the requests from the contract against your running server.
- Compare the actual responses from your API with the expected responses in the contract.
If every interaction matches, the test passes, and you can be confident that your consumer and provider will work together in production.
- Read the
-
Challenge
Step 6: Conclusion
Congratulations on completing the Pact Foundations Code Lab!
You have successfully:
- Implemented a client (Consumer) and an API (Provider)
- Defined the consumer's expectations as a contract using Pact
- Generated a
pact.jsonfile - Verified that the provider's implementation fulfills the contract
This process ensures that your services can evolve independently while maintaining confidence in their integrations. By catching breaking changes early in the development cycle, consumer-driven contract testing helps teams build more resilient and reliable microservice architectures.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.