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: we are currently in the process of replacing Pact Consumer JS DSL with this library. Please bare with us whilst we transition. If you want to use Pact with JS and are new to Pact, start here.
It's easy, simply run the below:
npm install --save-dev pact
The library provides a Verifier Service, Matchers and an API Interceptor:
Verifier Sets up a test double (Verifier Provider API) with expected interactions. It's also responsible for generating Pact files.
Matchers are functions you can use to increase the expressiveness of your tests, and reduce brittle test cases. See the matching docs for more information.
Interceptor is a utility that can be used to intercept requests to the Provider, where it's not simple for you to change your API endpoint.
To use the library on your tests, do as you would normally with any other dependency:
// ES6// you have to new the Interceptor// the others are just plain objectsconst interceptor =// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~// ES5var Pact =var matchers = PactMatchersmatchersmatchersmatchers// you have to new the Interceptorvar Interceptor =
Then to write a test that will generate a Pact file, here's an example below - it uses Mocha. There's a bit going on in there as we are spinning up the Pact Verifier Service Provider to mock a real server on the provider server. This is needed because that's where we will record our interactions.
More questions about what's involved in Pact? Read more about it.
examples folder for examples with Karma Jasmine / Mocha. The example below is taken from the integration spec.
// great library to spin up the Pact Verifier Server// that will record interactions and eventually validate your pacts
Once you have created Pacts for your Consumer, you need to validate those Pacts against your Provider.
First, install Pact Node:
npm install @pact-foundation/pact-node --save
Then run the Provider side verification step:
var pact = ;var 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. Optional;pact);
That's it! Read more about Verifying Pacts.
Sharing is caring - to simplify sharing Pacts between Consumers and Providers, checkout sharing pacts.
var pact = ;var 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. Optional;pact);
Check out Pact JS Mocha.
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!