13.1.4 • Published 2 months ago

@pact-foundation/pact v13.1.4

Weekly downloads
116,357
License
MIT
Repository
github
Last release
2 months ago

logo

Pact JS

Build Status npm Release workflow Coverage Status Code Climate Issue Count Known Vulnerabilities license slack

Fast, easy and reliable testing for your APIs and microservices.

Pact is the de-facto API contract testing tool. Replace expensive and brittle end-to-end integration tests with fast, reliable and easy to debug unit tests.

  • ⚡ Lightning fast
  • 🎈 Effortless full-stack integration testing - from the front-end to the back-end
  • 🔌 Supports HTTP/REST and event-driven systems
  • 🛠️ Configurable mock server
  • 😌 Powerful matching rules prevents brittle tests
  • 🤝 Integrates with Pact Broker / PactFlow for powerful CI/CD workflows
  • 🔡 Supports 12+ languages

Why use Pact?

Contract testing with Pact lets you:

  • ⚡ Test locally
  • 🚀 Deploy faster
  • ⬇️ Reduce the lead time for change
  • 💰 Reduce the cost of API integration testing
  • 💥 Prevent breaking changes
  • 🔎 Understand your system usage
  • 📃 Document your APIs for free
  • 🗄 Remove the need for complex data fixtures
  • 🤷‍♂️ Reduce the reliance on complex test environments

Watch our series on the problems with end-to-end integrated tests, and how contract testing can help.

----------

Documentation

This readme offers a basic introduction to the library. The full documentation for Pact JS and the rest of the framework is available at https://docs.pact.io/.

Tutorial (60 minutes)

Learn the key Pact JS features in 60 minutes: https://github.com/pact-foundation/pact-workshop-js

Need Help

Installation

npm i -S @pact-foundation/pact@latest

# 🚀 now write some tests!

Looking for the previous stable 9.x.x release?

Requirements

Node 16+ as of pact-js v12

  1. If using pact-js v11 or lower,
    1. make sure the ignore-scripts option is disabled, pact uses npm scripts to compile native dependencies and won't function without it.
    2. Pact uses native extensions and installs them via the node-gyp package. This requires a build chain for a successful installation. See also issue #899. This is now prebuilt in pact-js v12+

Do Not Track

In order to get better statistics as to who is using Pact, we have an anonymous tracking event that triggers when Pact installs for the first time. The only things we track are your type of OS, and the version information for the package being installed. No PII data is sent as part of this request. You can disable tracking by setting the environment variable PACT_DO_NOT_TRACK=1:

----------

Usage

Consumer package

The main consumer interface are the PactV3 class and MatchersV3 exports of the @pact-foundation/pact package.

Writing a Consumer test

Pact is a consumer-driven contract testing tool, which is a fancy way of saying that the API Consumer writes a test to set out its assumptions and needs of its API Provider(s). By unit testing our API client with Pact, it will produce a contract that we can share to our Provider to confirm these assumptions and prevent breaking changes.

In this example, we are going to be testing our User API client, responsible for communicating with the UserAPI over HTTP. It currently has a single method GetUser(id) that will return a *User.

Pact tests have a few key properties. We'll demonstrate a common example using the 3A Arrange/Act/Assert pattern.

import { PactV3, MatchersV3 } from '@pact-foundation/pact';

// Create a 'pact' between the two applications in the integration we are testing
const provider = new PactV3({
  dir: path.resolve(process.cwd(), 'pacts'),
  consumer: 'MyConsumer',
  provider: 'MyProvider',
});

// API Client that will fetch dogs from the Dog API 
// This is the target of our Pact test
public getMeDogs = (from: string): AxiosPromise => {
  return axios.request({
    baseURL: this.url,
    params: { from },
    headers: { Accept: 'application/json' },
    method: 'GET',
    url: '/dogs',
  });
};

const dogExample = { dog: 1 };
const EXPECTED_BODY = MatchersV3.eachLike(dogExample);

describe('GET /dogs', () => {
  it('returns an HTTP 200 and a list of dogs', () => {
    // Arrange: Setup our expected interactions
    //
    // We use Pact to mock out the backend API
    provider
      .given('I have a list of dogs')
      .uponReceiving('a request for all dogs with the builder pattern')
      .withRequest({
        method: 'GET',
        path: '/dogs',
        query: { from: 'today' },
        headers: { Accept: 'application/json' },
      })
      .willRespondWith({
        status: 200,
        headers: { 'Content-Type': 'application/json' },
        body: EXPECTED_BODY,
      });

    return provider.executeTest((mockserver) => {
      // Act: test our API client behaves correctly
      //
      // Note we configure the DogService API client dynamically to 
      // point to the mock service Pact created for us, instead of 
      // the real one
      dogService = new DogService(mockserver.url);
      const response = await dogService.getMeDogs('today')

      // Assert: check the result
      expect(response.data[0]).to.deep.eq(dogExample);
    });
  });
});

You can see (and run) the full version of this in ./examples/v3/typescript, as well as other examples in the parent folder.

To run the examples

  1. Clone the repo git@github.com:pact-foundation/pact-js.git

Run a single example

  1. Change into the required example folder cd examples/e2e/v3/typescript
  2. Install all the examples dependencies npm install
  3. Run all the example - npm run test

Run all examples

  1. Change into the examples folder cd examples
  2. Install all the examples dependencies ./installAll.sh
  3. Run all the examples ./runAll.sh

----------

Provider package

The main provider interface is the Verifier class of the @pact-foundation/pact package.

Verifying a Provider

A provider test takes one or more pact files (contracts) as input, and Pact verifies that your provider adheres to the contract. In the simplest case, you can verify a provider as per below using a local pact file, although in practice you would usually use a Pact Broker to manage your contracts and CI/CD workflow.

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

// (1) Start provider locally. Be sure to stub out any external dependencies
server.listen(8081, () => {
  importData();
  console.log('Animal Profile Service listening on http://localhost:8081');
});

// (2) Verify that the provider meets all consumer expectations
describe('Pact Verification', () => {
  it('validates the expectations of Matching Service', () => {
    let token = 'INVALID TOKEN';

    return new Verifier({
      providerBaseUrl: 'http://localhost:8081', // <- location of your running provider
      pactUrls: [ path.resolve(process.cwd(), "./pacts/SomeConsumer-SomeProvider.json") ],
    })
      .verifyProvider()
      .then(() => {
        console.log('Pact Verification Complete!');
      });
  });
});

It's best to run Pact verification tests as part of your unit testing suite, so you can readily access stubbing, IaC and other helpful tools.

----------

Compatibility

VersionStableSpec CompatibilityInstall
11.x.x +Yes2, 3, 4See installation
10.x.xYes2, 3, 4See installation
9.x.xYes2, 3*9xx

* v3 support is limited to the subset of functionality required to enable language inter-operable Message support.

OSArchitectureSupportedPact-JS Version
MacOSx86_64All
MacOSarm649.x +
Linux (libc)x86_64All
Linux (libc)arm6410.x +
Linux (musl)x86_6413.x +
Linux (musl)arm6413.x +
Windowsx86_64All
Windowsx869.x -
Windowsarm64-

Roadmap

The roadmap for Pact and Pact JS is outlined on our main website.

Contributing

See CONTRIBUTING.

13.1.4

2 months ago

13.1.3

2 months ago

13.1.1

4 months ago

13.1.2

3 months ago

13.1.0

5 months ago

12.5.2

6 months ago

12.5.1

6 months ago

13.0.0

5 months ago

12.5.0

7 months ago

12.4.0

8 months ago

12.3.1

8 months ago

12.3.0

9 months ago

12.2.0

10 months ago

12.1.3

10 months ago

12.1.2

11 months ago

12.0.0

1 year ago

12.1.0

1 year ago

12.1.1

1 year ago

11.0.2

2 years ago

11.0.0

2 years ago

11.0.1

2 years ago

10.4.1

2 years ago

10.4.0

2 years ago

10.3.0

2 years ago

10.3.1

2 years ago

10.2.0

2 years ago

10.2.1

2 years ago

10.2.2

2 years ago

10.1.4

2 years ago

10.1.3

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.1.0-beta.0

2 years ago

9.18.0

2 years ago

9.18.1

2 years ago

10.0.0-beta.60

2 years ago

10.0.0-beta.61

2 years ago

10.1.0

2 years ago

10.1.1

2 years ago

10.1.2

2 years ago

10.0.0-beta.62

2 years ago

10.0.0-beta.59

3 years ago

9.17.3

3 years ago

10.0.0-beta.57

3 years ago

10.0.0-beta.58

3 years ago

10.0.0-beta.55

3 years ago

10.0.0-beta.56

3 years ago

9.17.1

3 years ago

9.17.2

3 years ago

9.17.0

3 years ago

10.0.0-beta.54

3 years ago

9.16.5

3 years ago

9.16.4

3 years ago

10.0.0-beta.53

3 years ago

10.0.0-beta.52

3 years ago

9.16.3

3 years ago

9.16.2

3 years ago

10.0.0-beta.50

3 years ago

10.0.0-beta.51

3 years ago

10.0.0-beta.47

3 years ago

10.0.0-beta.48

3 years ago

10.0.0-beta.49

3 years ago

10.0.0-beta.46

3 years ago

10.0.0-beta.45

3 years ago

9.16.1

3 years ago

10.0.0-beta.44

3 years ago

10.0.0-beta.43

3 years ago

10.0.0-beta.42

3 years ago

10.0.0-beta.41

3 years ago

10.0.0-beta.39

3 years ago

10.0.0-beta.40

3 years ago

9.16.0

3 years ago

10.0.0-beta.38

3 years ago

10.0.0-beta.36

3 years ago

10.0.0-beta.37

3 years ago

10.0.0-beta.35

4 years ago

9.15.5

4 years ago

10.0.0-beta.34

4 years ago

9.15.4

4 years ago

9.15.3

4 years ago

10.0.0-beta.33

4 years ago

9.15.2

4 years ago

9.15.1

4 years ago

10.0.0-beta.32

4 years ago

10.0.0-beta.31

4 years ago

10.0.0-beta.30

4 years ago

9.15.0

4 years ago

10.0.0-beta.29

4 years ago

10.0.0-beta.27

4 years ago

9.14.2

4 years ago

9.14.1

4 years ago

10.0.0-beta.26

4 years ago

10.0.0-beta.24

4 years ago

10.0.0-beta.25

4 years ago

10.0.0-beta.23

4 years ago

9.14.0

4 years ago

9.13.2

4 years ago

9.13.1

4 years ago

10.0.0-beta.22

4 years ago

10.0.0-beta.21

4 years ago

10.0.0-beta.20

4 years ago

9.13.0

4 years ago

10.0.0-beta.19

4 years ago

10.0.0-beta.18

4 years ago

10.0.0-beta.17

4 years ago

9.12.2

4 years ago

10.0.0-beta.16

4 years ago

9.12.1

4 years ago

9.12.0

4 years ago

10.0.0-beta.15

4 years ago

10.0.0-beta.14

4 years ago

9.11.1

4 years ago

10.0.0-beta.13

4 years ago

10.0.0-beta.12

4 years ago

10.0.0-beta.11

4 years ago

10.0.0-beta.10

4 years ago

10.0.0-beta.9

4 years ago

10.0.0-beta.5

4 years ago

9.11.0

5 years ago

10.0.0-beta.4

5 years ago

9.10.0

5 years ago

9.9.12

5 years ago

9.9.5

5 years ago

9.9.3

5 years ago

10.0.0-beta.3

5 years ago

9.9.1-beta.1

5 years ago

9.9.1-beta.2

5 years ago

9.9.2

5 years ago

9.8.2

5 years ago

10.0.0-beta.2

5 years ago

10.0.0-beta.1

5 years ago

9.8.1

5 years ago

9.8.0

5 years ago

9.7.0

5 years ago

9.6.1

5 years ago

9.6.0

5 years ago

9.5.1

5 years ago

9.5.0

5 years ago

9.4.0

5 years ago

9.2.4

5 years ago

9.2.3

5 years ago

9.2.3-beta.0

5 years ago

9.2.2

5 years ago

9.2.1

5 years ago

9.2.0

5 years ago

9.1.1

5 years ago

9.1.0

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

8.2.6

5 years ago

8.2.4

6 years ago

8.2.3

6 years ago

8.2.2

6 years ago

8.2.0

6 years ago

8.1.2

6 years ago

8.1.0

6 years ago

8.0.5

6 years ago

8.0.4

6 years ago

8.0.2

6 years ago

8.0.1

6 years ago

7.4.0

6 years ago

7.3.0

6 years ago

7.2.0

6 years ago

7.1.0

6 years ago

7.0.4

6 years ago

7.0.3

6 years ago

7.0.2

6 years ago

8.0.0-beta

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

5.9.1

7 years ago

6.0.0-alpha.14

7 years ago

6.0.0-alpha.13

7 years ago

6.0.0-alpha.12

7 years ago

6.0.0-alpha.10

7 years ago

6.0.0-alpha.8

7 years ago

6.0.0-alpha.7

7 years ago

6.0.0-alpha.6

7 years ago

6.0.0-alpha.4

7 years ago

6.0.0-alpha.2

7 years ago

5.9.0

7 years ago

5.8.0

7 years ago

5.7.0

7 years ago

5.6.1

7 years ago

5.6.0

7 years ago

5.5.1

7 years ago

5.5.0

7 years ago

5.4.0

7 years ago

5.3.2

7 years ago

5.3.1

7 years ago

5.3.0

7 years ago

5.2.0

7 years ago

5.1.0

7 years ago

5.0.3-beta

7 years ago

5.0.0-beta

7 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

2.7.0

7 years ago