Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

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 platform
Lab Info
Level
Beginner
Last updated
Oct 10, 2025
Duration
45m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use.
Table of Contents
  1. 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 data
    • ProductAPI (The Provider): An API that serves product data

    Your goal is to create a contract that guarantees ProductClient and ProductAPI are 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 script
    

    Running the Applications

    Consumer Application

    The consumer is a client library that makes HTTP requests to the provider API.

    Run Consumer Tests:

    cd consumer
    npm test
    

    This 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 start
    

    The server will start on http://localhost:8081

    Run Provider Verification Tests:

    cd provider
    npm test
    

    This 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.

  2. 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 axios to handle the HTTP requests.

  3. 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:

    1. Defines the interactions (expected request/response pairs) your client will make
    2. Starts a mock server that will respond exactly as defined in the interactions
    3. 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.json file. This file is the contract.

  4. 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.json file. You'll use Express.js to create a simple server that serves product data from an in-memory store.

  5. 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:

    1. Read the pact.json file.
    2. Start your real Provider API server.
    3. Replay the requests from the contract against your running server.
    4. 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.

  6. 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.json file
    • 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

Pluralsight Code Labs offer an opportunity to get hands-on learning in real-time. Be it a Challenge, Sandbox, or Guided Lab, these provide the real world experience needed to succeed in your development role.

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.

Get started with Pluralsight