An idiomatic Node interface for the Pact mock service (Consumer) and Verification (Provider) process.
npm install @pact-foundation/pact-node --save
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
var pact = require('@pact-foundation/pact-node');
pact.logLevel('debug');
Mock servers are used by Pact to record interactions and create pact contracts.
var pact = require('@pact-foundation/pact-node');
var server = pact.createServer({
...
});
Options:
Parameter | Required? | Type | Description |
---|---|---|---|
port |
false | number | Port number that the server runs on, defaults to random available port |
host |
false | string | Host 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. |
log |
false | string | File to log output on relative to current working directory, defaults to none |
ssl |
false | boolean | Create a self-signed SSL cert to run the server over HTTPS , defaults to false |
sslcert |
false | string | Path to a custom self-signed SSL cert file, 'ssl' option must be set to true to use this option, defaults to none |
sslkey |
false | string | Path a custom key and self-signed SSL cert key file, 'ssl' option must be set to true to use this, defaults to none |
cors |
false | boolean | Allow CORS OPTION requests to be accepted, defaults to 'false' |
dir |
false | string | Directory to write the pact contracts relative to the current working directory, defaults to none |
spec |
false | number | The pact specification version to use when writing pact contracts, defaults to '1' |
consumer |
false | string | The name of the consumer to be written to the pact contracts, defaults to none |
provider |
false | string | The name of the provider to be written to the pact contracts, defaults to none |
pactFileWriteMode |
false | `"overwrite" | "update" |
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 servers once you're done with them in one fell swoop.
var pact = require('@pact-foundation/pact-node');
pact.removeAllServers();
Start the current server.
var pact = require('@pact-foundation/pact-node');
pact.createServer().start().then(function(){
// Do something after it started
});
Stop the current server.
var pact = require('@pact-foundation/pact-node');
pact.createServer().stop().then(function(){
// Do something after it stopped
});
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
});
var pact = require('@pact-foundation/pact-node');
pact.createServer().running;
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'); });
Read more about Verify Pacts.
var pact = require('@pact-foundation/pact-node');
pact.verifyPacts({
...
});
Options:
Parameter | Required? | Type | Description |
---|---|---|---|
providerBaseUrl |
true | string | Running API provider host endpoint. |
pactBrokerUrl |
false | string | URL to fetch the pacts if pactUrls not supplied |
provider |
false | string | Name of the provider if fetching from a Broker |
tags |
false | array | Array of tags, used to filter pacts from the Broker |
pactUrls |
false | array | Array of local Pact file paths or HTTP-based URLs (e.g. from a broker). Required if not using a Broker. |
providerStatesSetupUrl |
false | string | URL to send PUT requests to setup a given provider state |
pactBrokerUsername |
false | string | Username for Pact Broker basic authentication |
pactBrokerPassword |
false | string | Password for Pact Broker basic authentication |
publishVerificationResult |
false | boolean | Publish verification result to Broker |
customProviderHeaders |
false | array | Header(s) to add to provider state set up and pact verification |
providerVersion |
false | boolean | Provider version, required to publish verification result to Broker. Optional otherwise. |
timeout |
false | number | The duration in ms we should wait to confirm verification process was successful. Defaults to 30000. |
var pact = require('@pact-foundation/pact-node');
var opts = {
...
};
pact.publishPacts(opts).then(function () {
// do something
});
Options:
Parameter | Required? | Type | Description |
---|---|---|---|
pactFilesOrDirs |
false | array | Array of local Pact files or directories containing them. Required. |
pactBroker |
false | string | URL to fetch the provider states for the given provider API. Optional. |
pactBrokerUsername |
false | string | Username for Pact Broker basic authentication. Optional |
pactBrokerPassword |
false | string | Password for Pact Broker basic authentication. Optional, |
tags |
false | array | An array of Strings to tag the Pacts being published. Optional |
consumerVersion |
false | string | A string containing a semver-style version e.g. 1.0.0. Required. |
Stub servers create runnable APIs from existing pact files.
The interface is comparable to the Mock Server API.
var pact = require('@pact-foundation/pact-node');
var server = pact.createStub({
...
});
Options:
Parameter | Required? | Type | Description |
---|---|---|---|
pactUrls | true | array | List of local Pact files to create the stub service from |
port | false | number | Port number that the server runs on, defaults to random available port |
host | false | string | Host 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. |
log | false | string | File to log output on relative to current working directory, defaults to none |
ssl | false | boolean | Create a self-signed SSL cert to run the server over HTTPS , defaults to 'false' |
sslcert | false | string | Path to a custom self-signed SSL cert file, 'ssl' option must be set to true to use this option. Defaults false |
sslkey | false | string | Path 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 |
cors | false | boolean | Allow CORS OPTION requests to be accepted, defaults to 'false' |
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.
Running npm test
will execute the tests that has the *.spec.js
pattern.
Please search for potential answers or post question on our official Pact StackOverflow.