From the Pact website:
The Pact family of frameworks provide support for Consumer Driven Contracts testing.
A Contract is a collection of agreements between a client (Consumer) and an API (Provider) that describes the interactions that can take place between them.
Consumer Driven Contracts is a pattern that drives the development of the Provider from its Consumers point of view.
Pact is a testing tool that guarantees those Contracts are satisfied.
Read Getting started with Pact for more information on how to get going.
NOTE: This project supersedes Pact Consumer JS DSL.
It's easy, simply run the below:
npm install --save-dev pact
Check out Pact JS Mocha.
To use the library on your tests, add the pact dependency:
let Pact =
Pact interface provides the following high-level APIs, they are listed in the order in which they typically get called in the lifecycle of testing a consumer:
||See Pact Node documentation for options||
||Creates a Mock Server test double of your Provider API. If you need multiple Providers for a scenario, you can create as many as these as you need.|
||Start the Mock Server|
||Register an expectation on the Mock Server, which must be called by your test case(s). You can add multiple interactions per server. These will be validated and written to a pact if successful.|
||Verifies that all interactions specified|
||Records the interactions registered to the Mock Server into the pact file and shuts it down.|
||In some cases you might want to clear out the expectations of the Mock Service, call this to clear out any expectations for the next test run. NOTE:
The first step is to create a test for your API Consumer. The example below uses Mocha, and demonstrates the basic approach:
Check out the
examples folder for examples with Karma Jasmine, Mocha and Jest. The example below is taken from the integration spec.
let path =let chai =let pact =let request =let chaiAsPromised =let expect = chaiexpectchai;
Once you have created Pacts for your Consumer, you need to validate those Pacts against your Provider. The Verifier object provides the following API for you to do so:
||Start the Mock Server|
const verifier = Verifier;let opts =providerBaseUrl: <String> // Running API provider host endpoint. Required.pactUrls: <Array> // Array of local Pact file paths or Pact Broker URLs (http based). Required.providerStatesUrl: <String> // URL to fetch the provider states for the given provider API. Optional.providerStatesSetupUrl <String> // URL to send PUT requests to setup a given provider state. Optional.pactBrokerUsername: <String> // Username for Pact Broker basic authentication. OptionalpactBrokerPassword: <String> // Password for Pact Broker basic authentication. Optionaltimeout: <Number> // The duration in ms we should wait to confirm verification process was successful. Defaults to 30000, Optional.;verifier);
That's it! Read more about Verifying Pacts.
let pact = ;let opts =pactUrls: <Array> // Array of local Pact files or directories containing them. Required.pactBroker: <String> // URL to fetch the provider states for the given provider API. Optional.pactBrokerUsername: <String> // Username for Pact Broker basic authentication. OptionalpactBrokerPassword: <String> // Password for Pact Broker basic authentication. OptionalconsumerVersion: <String> // A string containing a semver-style version e.g. 1.0.0. Required.;pact);
git checkout -b my-new-feature)
git commit -am 'Add some feature')
git push origin my-new-feature)
The vision is to have a compatible
Pact implementation in all the commonly used languages, your help would be greatly appreciated!