Consumer-Driven Contract Testing with Pact – Code Example

Here’s a practical step-by-step guide on how to set up and use Pact with PactFlow for consumer-driven contract testing (CDC) between a React frontend web application (the consumer) and an Express.js REST API (the provider) in the context of a ProductsC…


This content originally appeared on DEV Community and was authored by Paul-Sebastian Manole

Here's a practical step-by-step guide on how to set up and use Pact with PactFlow for consumer-driven contract testing (CDC) between a React frontend web application (the consumer) and an Express.js REST API (the provider) in the context of a ProductsCatalog API for creating a new product.

This post serves as a more practical hands-on example for the introduction to how Pact CDC works that I wrote here.

Step 1: Set Up Pact in the Consumer (React App)

  1. Install Pact in the Consumer Project:
   npm install --save-dev @pact-foundation/pact
  1. Write a Consumer Pact Test: Create a test file (e.g., pactTests/CreateProductPact.spec.js) in your consumer project:
   const { Pact } = require('@pact-foundation/pact');
   const path = require('path');
   const { createProduct } = require('../src/api'); // Function that makes the API call

   const provider = new Pact({
     consumer: 'ProductsCatalogConsumer',
     provider: 'ProductsCatalogAPI',
     port: 1234,
     log: path.resolve(process.cwd(), 'logs', 'pact.log'),
     dir: path.resolve(process.cwd(), 'pacts'),
     logLevel: 'INFO',
     spec: 2,
   });

   describe('Products API Pact', () => {
     beforeAll(() => provider.setup());

     afterAll(() => provider.finalize());

     beforeEach(() => {
       const interaction = {
         state: 'provider allows product creation',
         uponReceiving: 'a request to create a product',
         withRequest: {
           method: 'POST',
           path: '/products',
           headers: { 'Content-Type': 'application/json' },
           body: {
             name: 'New Product',
             price: 29.99,
           },
         },
         willRespondWith: {
           status: 201,
           headers: { 'Content-Type': 'application/json' },
           body: {
             id: 1,
             name: 'New Product',
             price: 29.99,
           },
         },
       };
       return provider.addInteraction(interaction);
     });

     it('should create a product successfully', async () => {
       const response = await createProduct({ name: 'New Product', price: 29.99 });
       expect(response).toEqual({
         id: 1,
         name: 'New Product',
         price: 29.99,
       });
     });

     afterEach(() => provider.verify());
   });
  1. Run the Consumer Pact Test:
   jest pactTests/CreateProductPact.spec.js
  1. Publish the Pact File to PactFlow: Create a script (e.g., publishPact.js):
   const pact = require('@pact-foundation/pact-node');
   const path = require('path');

   pact.publishPacts({
     pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],
     pactBroker: '[https://your-pactflow-url](https://your-pactflow-url)',
     consumerVersion: '1.0.0',
     tags: ['prod'],
   });

Run the script:

   node publishPact.js

Step 2: Set Up Pact in the Provider (Express.js API)

  1. Install Pact in the Provider Project:
   npm install --save-dev @pact-foundation/pact
  1. Write a Provider Verification Test: Create a test file (e.g., pactTests/VerifyPacts.spec.js) in your provider project:
   const { Verifier } = require('@pact-foundation/pact');
   const path = require('path');
   const server = require('../src/server'); // Your Express app

   server.listen(8080, () => {
     console.log('Provider API listening on [http://localhost:8080'](http://localhost:8080'));
   });

   describe('Pact Verification', () => {
     it('validates the expectations of ProductsCatalogConsumer', () => {
       return new Verifier({
         provider: 'ProductsCatalogAPI',
         providerBaseUrl: '[http://localhost:8080](http://localhost:8080)',
         pactBrokerUrl: '[https://your-pactflow-url](https://your-pactflow-url)',
         providerVersion: '1.0.0',
         tags: ['prod'],
         publishVerificationResult: true,
       }).verifyProvider();
     });
   });
  1. Run the Provider Verification Test:
   jest pactTests/VerifyPacts.spec.js

Step 3: Configure PactFlow

  1. Set Up PactFlow:

    • Sign up for a PactFlow account and create a new workspace.
    • Obtain the PactFlow URL and authentication token.
  2. Configure CI/CD Pipelines:

    • Ensure your CI/CD pipeline runs the consumer tests, publishes the pacts to PactFlow, and then triggers the provider verification tests.

Summary of the Workflow

  1. Consumer (React App):

    • Write consumer tests using Pact.
    • Generate Pact files during test execution.
    • Publish Pact files to PactFlow.
  2. Provider (Express.js API):

    • Fetch Pact files from PactFlow.
    • Verify the provider against the Pact files.
    • Publish verification results back to PactFlow.

By following these steps, you ensure that both the consumer and provider are aligned through the contract, facilitating seamless integration and continuous delivery.


This content originally appeared on DEV Community and was authored by Paul-Sebastian Manole


Print Share Comment Cite Upload Translate Updates
APA

Paul-Sebastian Manole | Sciencx (2024-08-05T01:52:24+00:00) Consumer-Driven Contract Testing with Pact – Code Example. Retrieved from https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/

MLA
" » Consumer-Driven Contract Testing with Pact – Code Example." Paul-Sebastian Manole | Sciencx - Monday August 5, 2024, https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/
HARVARD
Paul-Sebastian Manole | Sciencx Monday August 5, 2024 » Consumer-Driven Contract Testing with Pact – Code Example., viewed ,<https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/>
VANCOUVER
Paul-Sebastian Manole | Sciencx - » Consumer-Driven Contract Testing with Pact – Code Example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/
CHICAGO
" » Consumer-Driven Contract Testing with Pact – Code Example." Paul-Sebastian Manole | Sciencx - Accessed . https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/
IEEE
" » Consumer-Driven Contract Testing with Pact – Code Example." Paul-Sebastian Manole | Sciencx [Online]. Available: https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/. [Accessed: ]
rf:citation
» Consumer-Driven Contract Testing with Pact – Code Example | Paul-Sebastian Manole | Sciencx | https://www.scien.cx/2024/08/05/consumer-driven-contract-testing-with-pact-code-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.