Skip to main content

Example NodeJS SNS Provider

Source Code

https://github.com/pactflow/example-provider-js-sns

Build Status

Can I deploy Status

Pact Status (latest pact)

Pact Status (prod/prod pact)

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 dXfltyFMgNOFZAxr8io9wJ37iUpY42M/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 product topic.

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:

SNS Architecture

Theory

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:

Code Modularity

This code base is setup with this modularity in mind (key files):

The target of our provider pact test is the Event Producer, which is responsible for producing a Product update event, that the Event Service will publish to SNS.

See also:

Pre-requisites

Software:

Usage

See also the PactFlow CI/CD Workshop for more background.

Testing

  • Run the Pact tests: make test

Running locally

  • Start the Provider API (with a local SNS setup with localstack): make start
  • Create a product: make create-product
  • Update a product: make update-product
  • Delete a product: make delete-product