Publishing Consumer Contract from Multiple Pact Files

Viewed 32

I'm new to Pact and Pactflow so any help would be greatly appreciated.

I have a provider application (written in TypeScript) that has about 50 endpoints. And a consumer application (also written in TypeScript) that calls these 50 endpoints. I want to generate pact files for the consumer from unit tests and save them under a directory called '/pacts'. Because of the number of endpoints I want to create multiple pact files.

I want to publish all these consumer pact files to Pactflow with the same contract version like the following:

const { Publisher } = require('@pact-foundation/pact');

export async function publishPact() {
  const opts = {
    pactBroker: 'https://myapp.pactflow.io',
    pactBrokerToken: '{token}',
    consumerVersion: '0.0.3',
    pactFilesOrDirs: ['./pacts'],
  };

  new Publisher(opts).publishPacts();
}

My pact files look something like this:

 {
  "consumer":{
    "name":"product-consumer"
  },
  "provider":{
    "name":"product-provider"
  },
  "interactions":[
    {
      "description":"Consumer contract products",
      "request":{
        "method":"GET",
        "path":"/api/v1/pages/product"
      },
      "response":{
        "status":200,
        "headers":{
          "connection":"close",
          "vary":"Accept-Encoding, User-Agent",
          "server":"Jetty(9.4.20.v20190813)"
        },
        "body":{
          "message":"SUCCESS",
          "data":{
            "elements":[
              // ommited..
            ]
          }
        }
      }
    }
  ],
  "metadata":{
    "pactSpecification":{
      "version":"2.0.0"
    }
  }
}

However, when I try to publish the contracts, I get the following error:

Cannot change the content of the pact for product-provider version 0.0.3 and product-product, as race conditions will cause unreliable results for can-i-deploy. Each pact must be published with a unique consumer version number.

Is this possible to do?

1 Answers

You can split tests across files, that's fine.

The error you are getting is that you are publishing the contract to a Pact Broker (Pactflow) with the same consumer version number.

Cannot change the content of the pact for product-provider version 0.0.3 and product-product, as race conditions will cause unreliable results for can-i-deploy. Each pact must be published with a unique consumer version number.

This error is very clear - each version should be unique. See this guide on versioning, but the TL;DR is that you should use something like the Git SHA or Tim Jones' excellent https://www.npmjs.com/package/absolute-version?activeTab=readme

Related