Pact-js: Pactjs Message example not sending Message object

Created on 1 Mar 2019  路  3Comments  路  Source: pact-foundation/pact-js

Software versions

  • OS: Windows 10
  • Consumer Pact library: Pact JS latest
  • Test library: jest
  • Node Version: 11.2.0

Expected behaviour

Should send Message object when doing consumer test and mocking pact as message queue

Actual behaviour

It send object as it as e.g {idd: Matchers.like(1),name: Matchers.like('rover'),type: Matchers.term({ generate: 'bulldog', matcher: '^(bulldog|sheepdog)$' }),} instead of Message object with content as mentioned in PactJs Message Example

Steps to reproduce

I am following the given example as following

const {
  Matchers,
  MessageConsumerPact,
  synchronousBodyHandler,
} = require('@pact-foundation/pact');

const path = require('path');

const processMsg = require('./../render');

describe('Message consumer tests', () => {
  const messagePact = new MessageConsumerPact({
    consumer: 'MyJSMessageConsumer',
    dir: path.resolve(process.cwd(), 'pacts'),
    pactfileWriteMode: 'update',
    provider: 'MyJSMessageProvider',
    logLevel: 'INFO',
  });

  describe('receive dog event', () => {
    it('accepts a valid dog', () => messagePact
      .given('some state')
      .expectsToReceive('a request for a dog')
      .withContent({
        id: Matchers.like(1),
        name: Matchers.like('rover'),
        type: Matchers.term({ generate: 'bulldog', matcher: '^(bulldog|sheepdog)$' }),
      })
      .withMetadata({
        'content-type': 'application/json',
      })
      .verify(synchronousBodyHandler(processMsg)));
  });

  // This is an example of a pact breaking
  // uncomment to see how it works!
  it.skip('Does not accept an invalid dog', () => messagePact
    .given('some state')
    .expectsToReceive('a request for a dog')
    .withContent({
      name: 'fido',
    })
    .withMetadata({
      'content-type': 'application/json',
    })
    .verify(processMsg));
});

m i doing something wrong or understanding the PactJs Message Example wrong?

The actual request body that Pact will send, will be contained within a Message object along with other context, so the body must be retrieved via content attribute.

Triage

All 3 comments

The function synchronousBodyHandler is what is doing this for you. It's a transformer, that means existing handlers that didn't know anything about a Message object could be wrapped easily.

If you want access to the fully Message, don't use the handler and just wrap it yourself. The handler is pretty simple - it takes a Message and returns a Promise

@mefellows thank for prompt reply, i am kinda new into pactjs, is it possible you can give an example of how i can "wrap it myself", i did achieved my goal doing as following (i am using RabbitMQ in dev/prod environment) but m not sure if its the right way to do so?

const msg = {
  content: JSON.stringify({
    id: Matchers.like(1),
    name: Matchers.like('rover'),
    type: Matchers.term({ generate: 'bulldog', matcher: '^(bulldog|sheepdog)$' }),
  }),
};

using asynchronousBodyHandler for promise

describe('receive dog event', () => { it('accepts a valid dog', () => messagePact .given('some state') .expectsToReceive('a request for a dog') .withContent(msg) .withMetadata({ 'content-type': 'application/json', }) .verify(asynchronousBodyHandler(processMsg))); });

It looks fine to me. Most times you won't need the full Message object, so that is why we created wrappers for your message handlers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mefellows picture mefellows  路  7Comments

AdrienDevillers picture AdrienDevillers  路  4Comments

bethesque picture bethesque  路  9Comments

tiago-dazn picture tiago-dazn  路  6Comments

mefellows picture mefellows  路  6Comments