Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @pact-foundation/pact-node with all npm packages installed. Try it out:

var pactNode = require("@pact-foundation/pact-node")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@pact-foundation/pact-node v6.11.0

A wrapper for the Ruby version of Pact to work within Node

Build Status npm npm npm

Pact Node

An idiomatic Node interface for the Pact mock service (Consumer) and Verification (Provider) process.

Installation

npm install @pact-foundation/pact-node --save

Usage

Simply require the library and call the create function to start the mock service

var pact = require('@pact-foundation/pact-node');
var server = pact.createServer({port: 9999});
server.start().then(function() {
    // Do your testing/development here
});

Or if you're using Typescript instead of plain old Javascript

import pact from "@pact-foundation/pact-node";
const server = pact.createServer({port: 9999});
server.start().then(() => {
    // Do your testing/development here
});

Or you can also use the CLI

$# pact mock --port 9999

To see the list commands possible with the CLI, simply ask for help $# pact --help

Documentation

Set Log Level

var pact = require('@pact-foundation/pact-node');
pact.logLevel('debug');

Mock Servers

Mock servers are used by Pact to record interactions and create pact contracts.

Create Mock Server

var pact = require('@pact-foundation/pact-node');
var server = pact.createServer({
    ...
});

Options:

ParameterRequired?TypeDescription
portfalsenumberPort number that the server runs on, defaults to random available port
hostfalsestringHost on which to bind the server on, defaults to 'localhost'. Supports '0.0.0.0' to bind on all IPv4 addresses on the local machine.
logfalsestringFile to log output on relative to current working directory, defaults to none
sslfalsebooleanCreate a self-signed SSL cert to run the server over HTTPS , defaults to false
sslcertfalsestringPath to a custom self-signed SSL cert file, 'ssl' option must be set to true to use this option, defaults to none
sslkeyfalsestringPath a custom key and self-signed SSL cert key file, 'ssl' option must be set to true to use this, defaults to none
corsfalsebooleanAllow CORS OPTION requests to be accepted, defaults to 'false'
dirfalsestringDirectory to write the pact contracts relative to the current working directory, defaults to none
specfalsenumberThe pact specification version to use when writing pact contracts, defaults to '1'
consumerfalsestringThe name of the consumer to be written to the pact contracts, defaults to none
providerfalsestringThe name of the provider to be written to the pact contracts, defaults to none
pactFileWriteModefalse`"overwrite""update"

List Mock Servers

If you ever need to see which servers are currently created.

var pact = require('@pact-foundation/pact-node');
var servers = pact.listServers();
console.log(JSON.stringify(servers));

Remove All Mock Servers

Remove all servers once you're done with them in one fell swoop.

var pact = require('@pact-foundation/pact-node');
pact.removeAllServers();

Start a Mock Server

Start the current server.

var pact = require('@pact-foundation/pact-node');
pact.createServer().start().then(function(){
    // Do something after it started
});

Stop a Mock server

Stop the current server.

var pact = require('@pact-foundation/pact-node');
pact.createServer().stop().then(function(){
    // Do something after it stopped
});

Delete a Mock server

Stop the current server and deletes it from the list.

var pact = require('@pact-foundation/pact-node');
pact.createServer().delete().then(function(){
    // Do something after it was killed
});

Check if a Mock server is running

var pact = require('@pact-foundation/pact-node');
pact.createServer().running;

Mock Server Events

There's 3 different events available, 'start', 'stop' and 'delete'. They can be listened to the same way as an EventEmitter.

var pact = require('@pact-foundation/pact-node');
var server = pact.createServer();
server.on('start', function() { console.log('started'); });
server.on('stop', function() { console.log('stopped'); });
server.on('delete', function() { console.log('deleted'); });

Provider Verification

Read more about Verify Pacts.

var pact = require('@pact-foundation/pact-node');

pact.verifyPacts({
    ...
});

Options:

ParameterRequired?TypeDescription
providerBaseUrltruestringRunning API provider host endpoint.
pactBrokerUrlfalsestringURL to fetch the pacts if pactUrls not supplied
providerfalsestringName of the provider if fetching from a Broker
tagsfalsearrayArray of tags, used to filter pacts from the Broker
pactUrlsfalsearrayArray of local Pact file paths or HTTP-based URLs (e.g. from a broker). Required if not using a Broker.
providerStatesSetupUrlfalsestringURL to send PUT requests to setup a given provider state
pactBrokerUsernamefalsestringUsername for Pact Broker basic authentication
pactBrokerPasswordfalsestringPassword for Pact Broker basic authentication
publishVerificationResultfalsebooleanPublish verification result to Broker
customProviderHeadersfalsearrayHeader(s) to add to provider state set up and pact verification
providerVersionfalsebooleanProvider version, required to publish verification result to Broker. Optional otherwise.
timeoutfalsenumberThe duration in ms we should wait to confirm verification process was successful. Defaults to 30000.

Pact Broker Publishing

var pact = require('@pact-foundation/pact-node');
var opts = {
    ...
};

pact.publishPacts(opts).then(function () {
    // do something
});

Options:

ParameterRequired?TypeDescription
pactFilesOrDirstruearrayArray of local Pact files or directories containing them. Required.
pactBrokertruestringURL of the Pact Broker to publish pacts to. Required.
consumerVersiontruestringA string containing a semver-style version e.g. 1.0.0. Required.
pactBrokerUsernamefalsestringUsername for Pact Broker basic authentication. Optional
pactBrokerPasswordfalsestringPassword for Pact Broker basic authentication. Optional,
tagsfalsearrayAn array of Strings to tag the Pacts being published. Optional

Stub Servers

Stub servers create runnable APIs from existing pact files.

The interface is comparable to the Mock Server API.

Create Stub Server

var pact = require('@pact-foundation/pact-node');
var server = pact.createStub({
    ...
});

Options:

ParameterRequired?TypeDescription
pactUrlstruearrayList of local Pact files to create the stub service from
portfalsenumberPort number that the server runs on, defaults to random available port
hostfalsestringHost on which to bind the server on, defaults to 'localhost'. Supports '0.0.0.0' to bind on all IPv4 addresses on the local machine.
logfalsestringFile to log output on relative to current working directory, defaults to none
sslfalsebooleanCreate a self-signed SSL cert to run the server over HTTPS , defaults to 'false'
sslcertfalsestringPath to a custom self-signed SSL cert file, 'ssl' option must be set to true to use this option. Defaults false
sslkeyfalsestringPath a custom key and self-signed SSL cert key file, 'ssl' option must be set to true to use this option false. Defaults to none
corsfalsebooleanAllow CORS OPTION requests to be accepted, defaults to 'false'

Contributing

To develop this project, simply install the dependencies and run npm run watch to for continual development, linting and testing when a source file changes.

Testing

Running npm test will execute the tests that has the *.spec.js pattern.

Questions?

Please search for potential answers or post question on our official Pact StackOverflow.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free