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

@pact-foundation/pact-core

Package Overview
Dependencies
Maintainers
0
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pact-foundation/pact-core

Core of @pact-foundation/pact. You almost certainly don't want to depend on this directly.

  • 15.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
128K
increased by8.7%
Maintainers
0
Weekly downloads
 
Created

What is @pact-foundation/pact-core?

@pact-foundation/pact-core is a core library for the Pact framework, which is used for consumer-driven contract testing. It allows developers to define the interactions between services in a contract, ensuring that both the consumer and provider adhere to the agreed-upon contract. This helps in catching integration issues early in the development cycle.

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

Creating a Pact

This code sample demonstrates how to create a Pact between a consumer and a provider. It sets up the provider, defines an interaction, and verifies that the interaction occurs as expected.

const { Pact } = require('@pact-foundation/pact-core');
const provider = new Pact({
  consumer: 'ConsumerService',
  provider: 'ProviderService',
});

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

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

Verifying a Pact

This code sample shows how to verify a Pact file against a running provider service. It uses the Verifier class to ensure that the provider adheres to the contract defined in the Pact file.

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

const verifier = new Verifier({
  providerBaseUrl: 'http://localhost:8080',
  pactUrls: ['path/to/pact.json'],
});

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

Publishing Pacts

This code sample demonstrates how to publish Pact files to a Pact Broker. The Publisher class is used to upload the Pact files, making them available for other services to verify.

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

const publisher = new Publisher({
  pactFilesOrDirs: ['path/to/pacts'],
  pactBroker: 'http://pact-broker-url',
  consumerVersion: '1.0.0',
});

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

Other packages similar to @pact-foundation/pact-core

Keywords

FAQs

Package last updated on 06 Sep 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc