pact

Failed to find native module error when running example from pact-js repo


I am running the consumer example from the pact-js repo. Reproducing the example below (I modified the file to avoid typescript):

import { PactV3, MatchersV3 } from '@pact-foundation/pact';
import path from 'path';

// Create a 'pact' between the two applications in the integration we are testing
const provider = new PactV3({
  dir: path.resolve(process.cwd(), 'pacts'),
  consumer: 'MyConsumer',
  provider: 'MyProvider',
});

// API Client that will fetch dogs from the Dog API
// This is the target of our Pact test
getMeDogs = (from) => {
  return axios.request({
    baseURL: this.url,
    params: { from },
    headers: { Accept: 'application/json' },
    method: 'GET',
    url: '/dogs',
  });
};

const dogExample = { dog: 1 };
const EXPECTED_BODY = MatchersV3.eachLike(dogExample);

describe('GET /dogs', () => {
  it('returns an HTTP 200 and a list of docs', () => {
    // Arrange: Setup our expected interactions
    //
    // We use Pact to mock out the backend API
    provider
      .given('I have a list of dogs')
      .uponReceiving('a request for all dogs with the builder pattern')
      .withRequest({
        method: 'GET',
        path: '/dogs',
        query: { from: 'today' },
        headers: { Accept: 'application/json' },
      })
      .willRespondWith({
        status: 200,
        headers: { 'Content-Type': 'application/json' },
        body: EXPECTED_BODY,
      });

    return provider.executeTest( async (mockserver) => {
      // Act: test our API client behaves correctly
      //
      // Note we configure the DogService API client dynamically to 
      // point to the mock service Pact created for us, instead of 
      // the real one
      dogService = new DogService(mockserver.url);
      const response = await dogService.getMeDogs('today')

      // Assert: check the result
      expect(response.data[0]).to.deep.eq(dogExample);
    });
  });
});

I created a new directory and did npm init. My package.json looks like:

{
  "name": "repo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@pact-foundation/pact": "^12.1.1"
  },
  "type": "module"
}

Few more things:

Running the above example (saved to cons.js) generates following error:

 [15:38:11.420] ERROR (28229): pact-core@14.0.5: Failed to find native module for darwin-x64: TypeError: Cannot read properties of undefined (reading 'pactffiInitWithLogLevel')

Any pointers on how to get this running would be helpful.


Solution

  • I had to upgrade the OS version on my laptop. I am no longer getting the error after I installed node, npm, Xcode developer tools after the OS upgrade.