3

I am trying to implement pact-node using typescript. (https://github.com/pact-foundation/pact-node). I am having some problems and the resulting errors messages are not very descriptive. It's probably something I am doing wrong in the setup, a lot of the documentation and examples available online use pact.js and there are some differences. Below is my code:

   const path = require('path');
   import { Pact } from '../../../node_modules/@pact-foundation/pact';
   import { Interaction, InteractionObject } from  '../../../node_modules/@pact-foundation/pact';
   import { expect } from 'chai';
   import { afterEach, before, beforeEach, describe, it, after } from 'mocha';
   import { myService } from '../../main/typescript/service/test-service';

    describe('My Pact Test', () => {
    const port = 5428;
    let service: myService;


   const provider = new Pact({
        port,
        log: path.resolve(process.cwd(), 'logs', 'pact.log'),
        dir: path.resolve(process.cwd(), 'pacts'),
        spec: 2,
        consumer: 'MyConsumer',
        provider: 'MyProvider',
        pactfileWriteMode: 'merge',
    });


    const EXPECTED_BODY = [{
        'auctionStartTime': 1549652248000,
        'auctionEndTime': 1549911448000,
        'resolveTime': 1539670248000,
        'openTimestamp': 1533496996000,
        'closeTimestamp': 1547804158000,
        'previewStartTime': 1549393048000,
    }];

    before(() => provider.setup());

    after(() => provider.finalize());

    afterEach(() => provider.verify());

        describe ('should get items ', () => {
        console.log ('message 1 ');

        before(() => {
            console.log ('message 2');
            return provider.addInteraction({
                state: 'item present in database,
                uponReceiving: 'a request for items',
                withRequest: {
                    method: 'GET',
                    path: 'path_to_my_api_endpoint,
                    headers: {
                        Accept: 'application/json',
                    },
                },
                willRespondWith: {
                    status: 200,
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: EXPECTED_BODY,
                },
            });
        });

        it('returns the correct response', (done) => {
            console.log ('message 3');
            service.getInfo('123', '123')
                .then((response: any) => {
                    expect(response.data).to.eql(EXPECTED_BODY);
                    done();
                });
        });
    });
})

However when I try to run this I get the following error:

  1) My Pact Test "before all" hook:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.


  2) My Pact Test "after all" hook:
     Error: Failed to get the current sub/segment from the context.
      at Object.contextMissingRuntimeError [as contextMissing] (node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
      at Object.getSegment (node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
      at Object.resolveSegment (node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19)
      at captureOutgoingHTTPs (node_modules/aws-xray-sdk-core/lib/patchers/http_p.js:67:31)
      at captureHTTPsRequest (node_modules/aws-xray-sdk-core/lib/patchers/http_p.js:152:12)
      at node_modules/popsicle/src/index.ts:126:30
      at new WrappedPromise (node_modules/async-listener/es6-wrapped-promise.js:13:18)
      at node_modules/popsicle/src/index.ts:112:16
      at propagateAslWrapper (node_modules/async-listener/index.js:502:23)
      at node_modules/async-listener/glue.js:188:31
      at node_modules/async-listener/index.js:539:70
      at node_modules/async-listener/glue.js:188:31
      at <anonymous>

Anybody got any idea what I am doing wrong? Or failing that, does anyone have an example of how they implemented pact using typescript?

Thanks!

user1523236
  • 1,403
  • 3
  • 20
  • 43

2 Answers2

4

Is there a reason why you're not using https://github.com/pact-foundation/pact-js?

Pact Node is a lower level library probably not ideally suited to what you're doing. Pact JS is the higher level DSL for tests as you've created.

There is a TypeScript example in there.

update: you might need to increase the timeout, it seems your system is taking longer than 2s to start the mock server and is bailing.

The second error described looks unrelated to pact.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
  • Thanks Matthew. Unfortunately that did not work. I am still getting the same error. I updated my original question with my updated code for perusal. – user1523236 May 16 '18 at 20:43
  • 2
    @user1523236 since Pact is a separate binary, it sometimes takes a while to spin up and be ready. I normally have my tests have a 15s timeout, but normally takes around 2-3 seconds normally. Because of this, it might be better for you to only spin up the pact mock service ones for all your tests in a `before` function, but clear our all interactions in an `afterEach` after every test so that everything runs much faster. – J_A_X May 19 '18 at 03:02
  • Hi Guys, Yep timeout was an issue with this. Thanks for your replies. – user1523236 May 28 '18 at 19:24
-1

Sometimes I have seen changing the node port also resolves the timeout issue.

Amit
  • 41
  • 2
  • 9