liveBook · Manning

Pact message function

As explained in Contract Testing in Action

MessageProviderPact is a Pact API construct that configures a provider of message interactions for contract verification.

MessageProviderPact configures the provider side of a Pact message contract. It supplies named message interactions, message bodies, metadata, provider identity, version details, and Pact Broker credentials for verification. section 8.7

A provider test defines each interaction as a message description mapped to providerWithMetadata. The function returns the message body and event metadata, while verify() retrieves associated contracts from the Pact Broker and checks them. section 8.7

A typical configuration establishes:

  • A message interaction such as “a movie add event.” section 8.7
  • A providerWithMetadata callback that creates the message body and attaches metadata such as kafka_topic. section 8.7
  • Provider identity, version, branch, log level, and Pact Broker URL and token. section 8.7
  • A test that calls provider.verify() to validate the configured provider message. section 8.7

Movie event provider

A movie event provider maps “a movie add event” to createMovie("The World's End", "2013") and adds the movies Kafka topic as metadata. The test then calls provider.verify(), allowing the provider implementation to verify the body and metadata published by the consumer contract. section 8.7

Listing 8.22 Message provider constructor
    const provider = new MessageProviderPact({
      messageProviders: {
        'a movie add event': providerWithMetadata(() => 
          createMovie("The World's End", "2013"), {
            kafka_topic: 'movies'
        }),
      },
      logLevel: 'info',
      provider: 'EventProducer',
      providerVersion: '1.0.0',
      providerVersionBranch: 'main',
      pactBrokerUrl: process.env.PACT_BROKER_URL,
      pactBrokerToken: process.env.PACT_BROKER_TOKEN,
    });

Inspect the complete constructor showing messageProviders, providerWithMetadata, provider versioning, and Pact Broker configuration. section 8.7

How Contract Testing in Action covers it

How deep it goes
Explains constructor configuration, message callbacks, metadata, Broker retrieval, and verify(), without detailing Pact Broker internals.
Examples
Uses a movie add event whose body comes from createMovie and whose metadata identifies the movies Kafka topic.
What it assumes
Assumes a Pact message interaction, provider message function, and Pact Broker configuration already exist.
What you can do after
Apply it via Pact to configure and run provider verification against contracts retrieved from a Pact Broker.
How it connects
Connects message contracts with provider verification, Pact Broker, event-driven architecture, Kafka, and asynchronous messaging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.