Hi! I’m new with pact js, recently I try to create...
# pact-js
s
Hi! I’m new with pact js, recently I try to create a contract test for my project, on my consumer side I already succeed to create a consumer test with pact file, but on my provider side I need to use event-driven structure to create provider test and I’m not sure it can used to match the pact file. Can someone help me please?
m
Can you please share your code or a bit more detail? Are you using any of the examples to work from?
s
Copy code
import { LogLevel, Verifier } from '@pact-foundation/pact';
import * as getParent from './get-parent';
import sinon from 'sinon';


let sandbox: any;
const LOG_LEVEL = process.env.LOG_LEVEL || 'DEBUG';

const mockId = {
  queryStringParameters: {
    organisationId: '12345abcde',
  },
};

describe('send organisation parent provider test', () => {
  before(() => {
    sandbox = sinon.createSandbox();
  });

  afterEach(() => {
    sandbox.restore();
  });

  it('send organisation parent id', () => {
    return new Verifier({
      provider: 'getOrganisationParentProvider',
      logLevel: LOG_LEVEL as LogLevel,
      providerBaseUrl: '<http://localhost:8081>',
      stateHandlers: {
        'has the organisation id 12345abcde': () => {
          const stubResponse = {
            Items: [
              {
                id: '12345abcde',
                name: 'Test Organisation',
                parentId: 'TestParentId',
              },
            ],
          };

          // Stub the send function of DynamoDBDocumentClient
          const stub = sandbox.stub(getParent, 'send');
          stub.returns({ promise: () => Promise.resolve(stubResponse) });

          return Promise.resolve({
            description: `state set get the organisation parent id`,
          });
        },
      },

      providerVersion: 'organisation-parent-id-1.0',
      pactBrokerUsername: 'tap-pact-broker',
      pactBrokerPassword: 'JGNqcsd3x2JESU1LHFSw',
      pactBrokerUrl: '<https://pact-broker/>',
      consumerVersionSelectors: [
        {
          matchingBranch: true,
        },
      ],
      publishVerificationResult: true,
      enablePending: true,
    })
      .verifyProvider()
      .then((output) => {
        console.log('Pact Verification Complete!');
        console.log(output);
      });
  });
I try to use the example from pact github, and to be honest they are also confused me. the process in the doc is quite different….
Copy code
const p = new MessageProviderPact({
    messageProviders: {
      'a organisation parent id': providerWithMetadata(
        () => getParent(mockId),
        {
          'content-type': 'application/json',
        }
      ),
    },

    stateHandlers: {
      'has the organisation id 12345abcde': () => {
        const stubResponse = {
          Items: [
            {
              id: '12345abcde',
              name: 'Test Organisation',
              parentId: 'TestParentId',
            },
          ],
        };

        // Stub the send function of DynamoDBDocumentClient
        const stub = sandbox.stub(DynamoDBDocumentClient.prototype, 'send');
        stub.returns({ promise: () => Promise.resolve(stubResponse) });

        return Promise.resolve(`state set get the organisation parent id`);
      },
    },

    logLevel: LOG_LEVEL as LogLevel,
    provider: 'getOrganisationParentProvider',
    providerVersion: 'organisation-parent-id-1.0',
    pactBrokerUsername: 'tap-pact-broker',
    pactBrokerPassword: 'JGNqcsd3x2JESU1LHFSw',
    pactBrokerUrl: '<https://pact-broker/>',

    consumerVersionSelectors: [
      {
        matchingBranch: true,
      },
    ],
  });
  describe('send organisation parent id', () => {
    it('sends valid organisation parent id', () => {
      return p.verify();
    });
  });
This is another code which is beasd on the example of event-driven. Currently both code complain that the pact file cannot be downloaded from pact broker
Copy code
14:18:04.651] INFO (36059): pact@11.0.2: Verifying provider
[14:18:04.665] INFO (36059): pact-core@13.13.8: Verifying Pacts.
[14:18:04.666] INFO (36059): pact-core@13.13.8: Verifying Pact Files
[14:18:04.667] DEBUG (36059): pact-core@13.13.8: Initalising native core at log level 'DEBUG'
2023-06-04T04:18:04.672803Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_new_for_application FFI function invoked
2023-06-04T04:18:04.676468Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_set_provider_info FFI function invoked
[14:18:04.677] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierSetFilterInfo' was not executed as it had non-fatal validation errors: None of PACT_DESCRIPTION, PACT_PROVIDER_STATE or PACT_PROVIDER_NO_STATE were set in the environment
2023-06-04T04:18:04.677706Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_set_provider_state FFI function invoked
2023-06-04T04:18:04.677868Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_set_verification_options FFI function invoked
2023-06-04T04:18:04.678127Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_set_publish_options FFI function invoked
[14:18:04.678] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierSetConsumerFilters' was not executed as it had non-fatal validation errors: Either no consumerFilters option provided, or the array was empty
[14:18:04.678] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierSetFailIfNoPactsFound' was not executed as it had non-fatal validation errors: No failIfNoPactsFound option provided
[14:18:04.678] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierAddCustomHeader' was not executed as it had non-fatal validation errors: No customProviderHeaders option provided
[14:18:04.678] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierAddDirectorySource' was not executed as it had non-fatal validation errors: No pactUrls option provided
2023-06-04T04:18:04.679144Z DEBUG ThreadId(01) pact_ffi::verifier: pact_ffi::verifier::pactffi_verifier_broker_source_with_selectors FFI function invoked
[14:18:04.680] DEBUG (36059): pact-core@13.13.8: the optional ffi function 'pactffiVerifierAddProviderTransport' was not executed as it had non-fatal validation errors: No additional provider transports provided
In the beginning, I also received these debug information, but I’m not sure it is relevant with my issue.
m
Nothing of issue there that I can see. DEBUG level statements are for helping diagnose issues, but shouldn’t be an error themselves (that’s what
ERROR
is for)
thanks for sharing though