Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

@pact-foundation/pact

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pact-foundation/pact

Pact for all things Javascript


Version published
Weekly downloads
149K
decreased by-4.04%
Maintainers
2
Weekly downloads
 
Created

What is @pact-foundation/pact?

@pact-foundation/pact is a consumer-driven contract testing tool for microservices and distributed systems. It allows you to define the interactions between service consumers and providers in a contract, which can then be used to verify that both sides adhere to the contract.

What are @pact-foundation/pact's main functionalities?

Consumer Pact

This code demonstrates how to set up a Pact mock provider for a consumer service. It defines an interaction where the provider is expected to return data when a GET request is made to the /data endpoint.

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

const provider = new Pact({
  consumer: 'ConsumerService',
  provider: 'ProviderService',
  port: 1234,
  log: path.resolve(process.cwd(), 'logs', 'pact.log'),
  dir: path.resolve(process.cwd(), 'pacts'),
  logLevel: 'INFO'
});

provider.setup().then(() => {
  // Define interactions
  provider.addInteraction({
    state: 'provider has data',
    uponReceiving: 'a request for data',
    withRequest: {
      method: 'GET',
      path: '/data',
      headers: { 'Accept': 'application/json' }
    },
    willRespondWith: {
      status: 200,
      headers: { 'Content-Type': 'application/json' },
      body: { key: 'value' }
    }
  });

  // Verify interactions
  return provider.verify();
}).finally(() => provider.finalize());

Provider Verification

This code demonstrates how to verify a provider against a Pact file. It uses the Verifier class to ensure that the provider service meets the expectations defined in the Pact file.

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

const opts = {
  providerBaseUrl: 'http://localhost:8080',
  pactUrls: ['path/to/pact-file.json']
};

new Verifier().verifyProvider(opts).then(output => {
  console.log('Pact Verification Complete!');
  console.log(output);
}).catch(e => {
  console.error('Pact Verification Failed: ', e);
});

Pact Broker Integration

This code demonstrates how to publish Pact files to a Pact Broker. It uses the Publisher class to upload the Pact files, making them available for provider verification.

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

const opts = {
  pactFilesOrDirs: ['path/to/pacts'],
  pactBroker: 'http://pact-broker-url',
  consumerVersion: '1.0.0'
};

new Publisher(opts).publishPacts().then(() => {
  console.log('Pacts successfully published!');
}).catch(e => {
  console.error('Failed to publish pacts: ', e);
});

Other packages similar to @pact-foundation/pact

FAQs

Package last updated on 24 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts