npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pact-js-mocha

v1.0.2-alpha

Published

Mocha Interface for Pact

Downloads

5

Readme

Pact Mocha Interface

Build Status Code Climate Issue Count Coverage Status npm

Implementation of a Mocha Interface to be used with pact-js.

From the Pact website:

The Pact family of frameworks provide support for Consumer Driven Contracts testing.

A Contract is a collection of agreements between a client (Consumer) and an API (Provider) that describes the interactions that can take place between them.

Consumer Driven Contracts is a pattern that drives the development of the Provider from its Consumers point of view.

Pact is a testing tool that guarantees those Contracts are satisfied.

Read Getting started with Pact for more information on how to get going.

How to use it

This package is not yet published to NPM so you will need to install it manually by modifying your package.json.

Installation

It's easy, simply run:

npm install --save-dev pact-js-mocha

What does this interface does

This Mocha interface abstracts some aspects of the usage of the DSL to make your test a bit cleaner and not having to worry about learning the Pact JS DSL.

We thought it was useful due to the fact that there is some boilerplate code needed to get the DSL up and going so abstracting all this made sense.

In a nutshell:

  • Provides two top level Pact() and PactProvider() block that works like a describe() block but with extra init functionality for Pact
  • Starts up a Pact Mock Server for you
  • Does the verification for you sending back the response of the request for your own assertions
  • Clean up all state and shuts down the mock servers created

Usage

The interface "extends" the Mocha BDD so all the original hooks still work as expected thus you don't have to mix and match your test runs.

Once the library is installed you have to tell Mocha to use it. To do that you can either create a mocha.opts file on your test folder and tell Mocha to use it, like this:

--require ./node_modules/pact-js-mocha/src/index.js
For Consumers only

You also have to tell Mocha to start the Pact Mock Server. The management of the Mock Server is up to you: you can either manage within the test file itself or as part of your test suite.

The example below shows how you can do the latter. To manage within your test suite have a look at this integration test on the Pact JS library itself.

To do that create a new file inside your test folder named specHelper.js and add the below to it:

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

// create mock server to listen on port 1234
var mockServer = wrapper.createServer({
  port: 1234,
  log: path.resolve(process.cwd(), 'logs', 'mockserver-ui.log'),
  dir: path.resolve(process.cwd(), 'pacts'),
  spec: 2
})

// start the mock server
mockServer.start().then(function () {
  // runs Mocha's test suite
  run()
})

Finally you need to also tell Mocha to require specHelper.js and delay the execution. Your final set of arguments will look like this:

--require ./node_modules/pact-js-mocha/src/index.js
--require ./test/specHelper.js
--delay

That's it. Then you can write your consumer test like below:

var expect = require('chai').expect
var request = require('superagent')

var PactOpts = {
  consumer: 'PactUI',             // the name of your consumer
  provider: 'Projects Provider',  // the name of your Provider
  providerPort: 1234              // the port on which the provider runs
}

PactConsumer(PactOpts, function () {

  // this is wrapped in a before() block
  // it takes an Array of interactions
  addInteractions([{
    state: 'i have a list of projects',
    uponReceiving: 'a request for projects',
    withRequest: {
      method: 'get',
      path: '/projects',
      headers: { 'Accept': 'application/json' }
    },
    willRespondWith: {
      status: 200,
      headers: { 'Content-Type': 'application/json; charset=utf-8' },
      body: { reply: 'hello' }
    }
  }])

  function requestProjects () {
    return request.get('http://localhost:' + PactOpts.providerPort + '/projects').set({ 'Accept': 'application/json' })
  }

  // this is your 'it' block
  verify('a list of projects is returned', requestProjects, function (result, done) {
    expect(JSON.parse(result)).to.eql({ reply: 'hello' })
    done()
  })

  // this is wrapped in a after block
  // thus it runs after all your verify's
  // it writes the pact and clear all interactions
  finalizePact()

})

And your provider test will look like this (there's no need to tell Mocha about the Mock Server):

var expect = require('chai').expect

PactProvider({consumer: 'Projects Consumer', provider: 'Projects Provider'}, function () {

  var pactOpts = {
    providerBaseUrl: 'http://my.provider.com',
    pactUrls: '/path/to/pact_file.json',
    providerStatesUrl: 'http://my.provider.com/providerStates',
    providerStatesSetupUrl: 'http://my.provider.com/setupProviderStates',
  }

  honourPact(pactOpts, function (result, done) {
    done()
  })

})

Contact

  • Twitter: @pact_up
  • Google users group: https://groups.google.com/forum/#!forum/pact-support