Search code examples
node.jspact

Writing interactions for pact-js-mocha that respond with an error


I've been working through the pact-js-mocha example and I'm having some difficulty verifying an interaction when the expected response is an error. This is the interaction I would like to verify:

PactConsumer(PactOpts, function () {

  addInteractions([{
    state: 'i have a list of projects',
    uponReceiving: 'a bad request for projects',
    withRequest: {
      method: 'get',
      path: '/projects'
    },
    willRespondWith: {
      status: 400,
      headers: { 'Content-Type': 'application/json; charset=utf-8' },
      body: { reply: 'this is a 400' }
    }
  }])


  verify('a 400 is returned', expectError, function (result, done) {
    expect(JSON.parse(result)).to.eql({ reply: 'this is a 400' })

  })

  finalizePact()

})

However I'm not sure about the expectError() function. In the examples this returns a superagent request however when the status is set to 400 in the interaction the method seems to throw the error.

I've tried a few things but it has mostly been trail and all been error (things like using supertest to create a request and expecting on it's result).

Thanks for your help


Solution

  • One possible work around that has done the job for me:

    const chai = require('chai');
    const expect = require('chai').expect;
    chai.use(require('chai-as-promised'));
    var should = chai.should();
    const request = require('superagent');
    const commons = require('./specCommons');
    
    const EXPECTED_BODY = {
          "errors": [
              "invalid request"
          ]
    };
    const SENT_BODY = {
        "body": null
    };
    
    PactConsumer(commons.PACT_OPTS, function() {
    
      addInteractions([{
          state: 'the state',
          uponReceiving: 'an invalid request,
          withRequest: {
              method: 'POST',
              path: commons.PATH,
              headers: commons.REQUEST_HEADERS,
              body: SENT_BODY
          },
          willRespondWith: {
              status: 400,
              headers: commons.RESPONSE_HEADERS,
              body: EXPECTED_BODY
          }
      }]);
    
      function requestTemplate() {
    
          return request.post('http://localhost:' + commons.PACT_OPTS.providerPort + commons.PATH)
              .set(commons.REQUEST_HEADERS)
              .send(SENT_BODY)
              .catch((error) => {
                  return JSON.stringify(error);
              });
      }
    
      verify('a 400 error is returned for an invalid request', requestTemplate, 
      function(result, done) {
              result = JSON.parse(result);
              Promise.all([                
           expect(result.response.text).to.equal(JSON.stringify(EXPECTED_BODY)),
           expect(result.status).to.equal(400)
              ]).should.notify(done);
          });
    
      finalizePact();
    });
    

    Where the specCommons.js looks like:

    module.exports = {
      PATH: 'api endpoint',
      REQUEST_HEADERS: {
          'Accept': 'application/json'
      },
      RESPONSE_HEADERS: {
          'Content-Type': 'application/json; charset=utf-8'
      },
      PACT_OPTS: {
          consumer: 'our consumer',
          provider: 'our provider',
          providerPort: 1234
      }
    };