This is an example of a NodeJS provider that uses Pact, Pactflow and GitHub Actions to ensure that it is compatible with the expectations its consumers have of it.
It is using a public tenant on Pactflow, which you can access here using the credentials
O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1. The latest version of the Example Consumer/Example Provider pact is published here.
In the following diagram, we'll be testing the "Product Update API", a simple HTTP service that receives product updates via a REST API and publishes product events on the
We need to be able to test that we are able to produce valid events to the SNS topic that matches what the consumer(s) can handle:
Modern distributed architectures are increasingly integrated in a decoupled, asynchronous fashion. Message queues such as ActiveMQ, RabbitMQ, SNS, SQS, Kafka and Kinesis are common, often integrated via small and frequent numbers of microservices (e.g. lambda).
Pact supports these use cases, by abstracting away the protocol and focussing on the messages passing between them.
To reiterate: Pact does not know about the various message queueing technologies - there are simply too many! And more importantly, Pact is really about testing the messages that pass between them, you can still write your standard functional tests using other frameworks designed for such things.
When writing tests, Pact takes the place of the intermediary (MQ/broker etc.) and confirms whether or not the consumer is able to handle a given event, or that the provider will be able to produce the correct message.
How to write tests?
We recommend that you split the code that is responsible for handling the protocol specific things - in this case the SNS publishing code - and the piece of code that actually produces payload.
You're probably familiar with layered architectures such as Ports and Adaptors (also referred to as a Hexagonal architecture). Following a modular architecture will allow you to do this much more easily:
This code base is setup with this modularity in mind (key files):
See also the Pactflow CI/CD Workshop for more background.
- Run the Pact tests:
- Start the Provider API (with a local SNS setup with localstack):
- Create a product:
- Update a product:
- Delete a product: