mercredi 19 septembre 2018

Error with Typescript Pact.io test: PopsicleError: Unable to connect to

Expected: Running npm run pactTest should generate a pact file (JSON).

Results: I get an Unable to connect error.

Pact.io JavaScript implementation guide.

Pact.io Typescript test example.

Appreciate any thoughts or ideas as to what I'm doing wrong :)


The Error

FAIL src/services/api/TotalPayout.test.pact.ts The API getUsersTotalPayout ✕ Should call getUsersTotalPayout and return an object with the total_payout (45ms)

● The API › getUsersTotalPayout › Should call getUsersTotalPayout and return an object with the total_payout

PopsicleError: Unable to connect to "http://127.0.0.1:12345/interactions" Caused by: Error: connect ECONNREFUSED 127.0.0.1:12345

 at Request.Object.<anonymous>.Request.error (node_modules/popsicle/src/request.ts:91:12)
  at ClientRequest.<anonymous> (node_modules/popsicle/src/index.ts:218:31)

package.json script:

"pactTest": "export NODE_ENV=pactTest && jest --testRegex \"/*(.test.pact.ts)\" --runInBand --setupFiles ./pactSetup.ts --setupTestFrameworkScriptFile ./pactTestWrapper.ts",

My src/pactSetup.ts file

// @ts-ignore
import path from 'path';
import { Pact } from '@pact-foundation/pact/pact';
​
// @ts-ignore
global.provider = new Pact({
  port: 1234,
  log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
  dir: path.resolve(process.cwd(), 'pacts'),
  spec: 2,
  cors: true,
  pactfileWriteMode: 'update',
  consumer: 'Exchange',
  provider: 'LP Service'
});

My src/pactTestWrapper.ts

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; // This is to give the pact mock server time to start
​
// @ts-ignore
beforeAll(() => provider.setup()); // Create mock provider
// @ts-ignore
afterEach(() => provider.verify()); // Ensure the mock provider verifies expected interactions for each test
// @ts-ignore
afterAll(() => provider.finalize()); // Tear down the mock and write the pact

The test: src/services/api/TotalPayout.test.pact.ts

// @ts-ignore
import path from 'path';
import { Pact } from '@pact-foundation/pact';
import { getTotalPayout } from './apiPayout';

const port = 12345;
const endpoint = '/frontoffice/api/get-total-payout';

const EXPECTED_BODY = {
  total_payout: 100.21,
};

const userId = 'foo';

const provider = new Pact({
  port,
  log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
  dir: path.resolve(process.cwd(), 'pacts'),
  spec: 2,
  consumer: 'Exchange',
  provider: 'LP Service',
  pactfileWriteMode: 'merge'
});
​​
describe('The API', () => {
  // Copy this block once per interaction under test
  describe('getUsersTotalPayout', () => {
    beforeEach(() => {
      const interaction = {
        uponReceiving: 'a GET request with a user id',
        withRequest: {
          method: 'GET',
          path: endpoint,
          headers: {
            Accept: 'application/json',
          },
        },
        willRespondWith: {
          status: 200,
          headers: {
            'Content-Type': 'application/json'
          },
          body: EXPECTED_BODY
        },
      };

      // @ts-ignore
      return provider.addInteraction(interaction);
    });
​
    // add expectations
    it('Should call getUsersTotalPayout and return an object with the total_payout', done => {
      getTotalPayout(userId)
        .then((response: any) => {
          expect(response).toEqual(EXPECTED_BODY);
        })
        .then(done);
    });
  });
});

The api service file apiPayout.ts

// @ts-ignore
import axios, * as others from 'axios';

const endpoint = '/frontoffice/api/';

export const getTotalPayout = async (userId: string) => {
  const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
  return response.data;
};

Aucun commentaire:

Enregistrer un commentaire