Pact-js: Getting Started Difficult

Created on 26 Jan 2018  路  14Comments  路  Source: pact-foundation/pact-js

Software versions

  • OS: Using Docker
  • Consumer Pact library: @pact-foundation/pact v5.3.2
  • Provider Pact library: ?
  • Node Version: 8.9.4

Expected behaviour

Copy example tests from GitHub examples, and be able to run tests.

Actual behaviour

Tests fail for various reasons.

  • ERRCONNREFUSED
  • Pact is not a function
  • Pact is not a constructor
  • etc

Steps to reproduce

Dockerfile

FROM node:8

ADD . /app
WORKDIR /app

RUN npm install

CMD /bin/bash

package.json

{
  "name": "consumer",
  "version": "1.0.0",
  "description": "The Pact consumer.",
  "main": "index.js",
  "scripts": {
    "test": "node node_modules/.bin/mocha tests/"
  },
  "author": "Matt Canty",
  "license": "ISC",
  "devDependencies": {
    "@pact-foundation/pact": "^5.3.2",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "mocha": "^5.0.0"
  },
  "dependencies": {
    "axios": "^0.17.1"
  }
}

tests/index.js

https://github.com/pact-foundation/pact-js/blob/master/README.md#example

Relevant log files

Not sure which logs will be helpful. Hitting multitudes of errors. I think I'm probably just missing the mark completely...

Awaiting feedback Triage

All 14 comments

I'll take a look at this a bit later @mattcanty, but note that this example relies on dependencies from the root project, that may be a cause of some of these issues (i.e. an npm install in the root directory is a prerequisite)

I've got a working local docker example for you, will push up shortly (I recently upgraded my Mac, and can't push to GH... bah)

ok, reinstalled git etc. and we're good - see https://github.com/pact-foundation/pact-js/tree/feat/docker-example for an example working docker setup.

Needs some tidying up, but i split the base image so that I can create some docker builds down the track for each of the example projects.

I returned to v4.x.x and found more luck there. I am experiencing flaky tests. Sometimes due to the port being unreleased and sometimes because it is taking over 10 seconds.

Otherwise I think I'm more or less up and running now. I expect I'll be able to migrate to the newer version at some point soon too. I wonder if the README is up to date?

In the docker setup or native? Could you share your revised setup and I could take a look? Docs should be up to date, but that doesn't mean they are perfect! 馃槈

Yes. Give me a day or so and I'll publish working samples.

No probs, I'll mark the ticket for review to get back to.

If you have feedback on the docs and how/where they are confusing, let us know - in theory these should be the easiest to fix! (PR's of course are welcome).

How'd you go @mattcanty, did my branch help?

Hey! Yes. Apologies for not getting back sooner. Unfortunately the work I've been doing is on a private repository. Happy to show snippets but I don't think I should go publishing the whole repo.

The aim of my project is to run a demo of how we can use Pact. I've used Docker Compose as a way of showing the steps involve - and of course it's easier on dependencies.

./docker-compose.yml

version: '3'

volumes:
  postgres-data:
    driver: local

services:

  consumer-tests:
    build: consumer
    volumes:
      - ./consumer:/app
    command: npm t

  publish-pacts:
    build: consumer
    links:
      - pact-broker
    volumes:
      - ./consumer:/app
    command: npm run pact:publish

  provider-tests:
    build: provider
    links:
      - pact-broker
    volumes:
      - ./provider:/app
    command: dotnet test HelloWorldApiTests
    environment:
      - PACT_BROKER_URI=http://pact-broker
      - BUILD_NUMBER=0.0.1

  pact-broker:
    image: dius/pact-broker
    ports:
      - 80:80
    links:
      - postgres
    environment:
      PACT_BROKER_DATABASE_USERNAME: postgres
      PACT_BROKER_DATABASE_PASSWORD: password
      PACT_BROKER_DATABASE_HOST: postgres
      PACT_BROKER_DATABASE_NAME: postgres

  postgres:
    image: postgres
    healthcheck:
      test: psql postgres --command "select 1"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: postgres

I'll omit the provider side - although let me know if you're interested. I managed to write my own .Net Core version of the example test from https://github.com/pact-foundation/pact-net.

The test file ended up pretty much exactly as you had shown in your documentation.

./consumer/tests/index.spec.js

const path = require('path')
const chai = require('chai')
const pact = require('pact')
const chaiAsPromised = require('chai-as-promised')
const helloWorldApp = require('..')

const mockProviderUrl = 'http://localhost'
const mockProviderPort = 6004

chai.use(chaiAsPromised);

describe('Hello World API', () => {
  const provider = pact({
    consumer: 'MyApp',
    provider: 'HelloWorldApi',
    url: mockProviderUrl,
    port: mockProviderPort,
    log: path.resolve(process.cwd(), 'logs', 'pact.log'),
    dir: path.resolve(process.cwd(), 'pacts'),
    logLevel: 'INFO',
    spec: 2
  })

... omitted for brevity ...

And my dependencies:

./consumer/package.json

...
"devDependencies": {
    "@pact-foundation/pact-node": "^6.7.4",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "mocha": "^5.0.0",
    "pact": "^4.3.2"
  },
  "dependencies": {
    "axios": "^0.17.1"
  }
...

Let me know if you have any specific questions. I'm going to be doing a spike into Swagger as well now. The plan will also to be to bring up the version of Pact node to the latest. May be in touch with you again if I get that not a constructor error again!

Thanks

Just managed to figure out the problem with latest version of pact-js. I was using /pact-node instead of /pact.

It's pretty confusing. I have only one small alteration to make to the readme - https://github.com/pact-foundation/pact-js/pull/148.

Perhaps more clarity into the differences between pact-js and pact-node.

Love the Docker setup Matt, keen to see where you land if a blog post or project can come out of it. At a cursory glance, it looks good!

As for the pact-node confusion, I'm all ears - we actually had an issue just yesterday related to that.

Hi @mattcanty, did you have any feedback we should be putting into the README or can I close this? Any help would be of course, muchly appreciated!

Nothing at the moment. I just have that PR with the slight tweak to the example code in the README - #148

Thanks for your help. We'll be adopting a Pact approach soon!

No worries - closing this off (and have merged your change). Thanks!

Was this page helpful?
0 / 5 - 0 ratings