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() {
});
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(() => {
});
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');
Create Pact Mock Server
var pact = require('@pact-foundation/pact-node');
var server = pact.createServer({
port: <Number>,
host: <String>,
log: <String>,
ssl: <Boolean>,
sslcert: <String>,
sslkey: <String>,
cors: <Boolean>,
dir: <String>,
spec: <Number>,
consumer: <String>,
provider: <String>
});
Run Provider Verification
Read more about Verify Pacts.
var pact = require('@pact-foundation/pact-node');
pact.verifyPacts({
providerBaseUrl: <String>,
pactBrokerUrl: <String>
provider: <String>
tags: <Array>
pactUrls: <Array>,
providerStatesSetupUrl: <String>,
pactBrokerUsername: <String>,
pactBrokerPassword: <String>,
publishVerificationResult: <Boolean>
providerVersion: <Boolean>
timeout: <Number>
});
Publish Pacts to a Broker
var pact = require('@pact-foundation/pact-node');
var opts = {
pactUrls: <Array>,
pactBroker: <String>,
pactBrokerUsername: <String>,
pactBrokerPassword: <String>,
tags: <Array>,
consumerVersion: <String>
};
pact.publishPacts(opts)).then(function () {
});
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 server
Start the current server.
var pact = require('@pact-foundation/pact-node');
pact.createServer().start().then(function(){
});
Stop a Mock server
Stop the current server.
var pact = require('@pact-foundation/pact-node');
pact.createServer().stop().then(function(){
});
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(){
});
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'); });
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.