Skip to main content

Example Consumer (record and replay)

Source Code#

NOTE: if you're running the CI/CD workshop, you can ignore this section. This is an extension to the example that demonstrates a new feature in developer preview.

This is an example of a Node consumer using Pact to create a consumer driven contract, and sharing it via Pactflow.

It implements a "Product" website, to demonstrate the new bi-directional contract capability of Pactflow (previously referred to as Provider driven contracts, or collaborative contracts). See the Provider counterpart.

It is using a public tenant on Pactflow, which you can access here using the credentials dXfltyFMgNOFZAxr8io9wJ37iUpY42M/O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1. The latest version of the Example Consumer/Example Provider pact is published here.

In the following diagram, you can see how the consumer testing process works - it's the same as the current Pact process! (We do show an alternative using Nock's record/replay functionality)

When we call "can-i-deploy" the cross-contract validation process kicks off on Pactflow, to ensure any consumer consumes a valid subset of the OAS for the provider.

Consumer Test

When you run the CI pipeline (see below for doing this), the pipeline should perform the following activities (simplified):

Consumer Pipeline



Environment variables#

To be able to run some of the commands locally, you will need to export the following environment variables into your shell:


Pact use case#

  • make test - run the pact test locally
  • make fake_ci - run the CI process locally

BYO Tool use case with Nock (record/replay example)#

NOTE: The nock recordings are already in the project, in the ./fixtures directory, see below for how to obtain these recordings.

  • make clean - ensure previous pacts are cleared
  • make test_nock - run the nock test locally
  • make fake_ci_nock - run the nock version of the CI process locally

Re-record nock fixtures

You first need to start up the provider API in order to obtain nock recordings. The API must be running on http://localhost:3000 for this step to work.

For the default Provider designed for this workshop, you can simply start it up by running npm start in the root directory of the provider project, as per its README.

  • npm run test:record - this will run nock in record mode, and your api client will issue real requests to the API
  • npm run test:nock - run the nock tests in replay only mode, validating all stubs were used in the process, and writing a pact file if successful