pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.58k stars 342 forks source link

TypeError: ffi.pactffiMessageGivenWithParams is not a function #1200

Closed Kampfmoehre closed 3 months ago

Kampfmoehre commented 3 months ago

Software versions

Issue Checklist

Please confirm the following:

Expected behaviour

Tests run like before.

Actual behaviour

Tests fail with an error message:

TypeError: ffi.pactffiMessageGivenWithParams is not a function

      150 |
      151 |       await converterPact
    > 152 |         .given({ name: "A file to convert", params: { mediaType: "image" } })
          |          ^
      153 |         .expectsToReceive("A valid convert image request")
      154 |         .withMetadata({
      155 |           // "content-type": "application/json",

      at Object.givenWithParams (node_modules/@pact-foundation/pact-core/src/consumer/index.ts:46:9)
      at MessageConsumerPact.Object.<anonymous>.MessageConsumerPact.given (node_modules/@pact-foundation/src/messageConsumerPact.ts:77:20)
      at Object.<anonymous> (test/some.test.ts:152:10)

Steps to reproduce

We are having trouble running our tests in CI since the update of @pact-foundation/pact to version 12.1.2. Consumer message pact tests fail with the below error. Reverting to 12.1.2 lets the test run successfully again.

Tests run with Jest look like this:

it("something works", async () => {
  const messageRequest = {
    someId: Matchers.string("Nkiwmu-oFoRN3lqzYD7M"),
    originalFileChecksum: {
      algo: "SHA256",
      hash: Matchers.hexadecimal("b3eb64161ac2a85bf364fc3b0e19e063bc9375475237feddee2ae36089c59cb3"),
    },
    someOtherId: Matchers.string("T-PlRCvy2N3JnjAiANGC"),
    fileName: Matchers.string("some-file.bmp"),
    profile: Matchers.string("thumb"),
    targetFormat: Matchers.string("jpg"),
  };

  let result: MessageResponse | undefined = undefined;
  let message: Message | undefined;
  const ackMock = jest.fn();
  const nackMock = jest.fn();

  await converterPact
    .given({ name: "A file to convert", params: { mediaType: "image" } })
    .expectsToReceive("A valid convert image request")
    .withMetadata({
      type: "convert:image",
    })
    .withContent(converterRequest)
    .verify(async (m: PactMessage) => {
      message = new Message(m.contents, m.metadata);
      message.ack = ackMock;
      message.nack = nackMock;

      const channel = amqpService.getChannelFromPool("Exchange/Queue/convert");
      result = await (channel.consumer as Pipeline<Message, MessageResponse>).execute(message);

      return result;
    });

  expect(message).not.toBeUndefined();
  expect(nackMock).not.toHaveBeenCalled();
  expect(ackMock).toHaveBeenCalled();
  expect(result).not.toBeUndefined();
});

Relevant log files

No special logs are emitted from Pact, however running the same test with Pact version 12.1.2 (where the test works) emits the following logs:

[08:39:20.192] INFO (24436): 0.4.16: pact native library successfully found, and the correct version
2024-03-22T07:39:20.198657Z  WARN ThreadId(01) pact_ffi::mock_server::handles: Failed to parse metadata value 'convert:image' as JSON - expected value at line 1 column 1. Will treat it as string
2024-03-22T07:39:20.198776Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $
2024-03-22T07:39:20.198787Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Configuring a normal object
2024-03-22T07:39:20.198909Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.convertedFileId
2024-03-22T07:39:20.198913Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
2024-03-22T07:39:20.198932Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.originalFileChecksum
2024-03-22T07:39:20.198934Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Configuring a normal object
2024-03-22T07:39:20.198938Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.originalFileChecksum.hash
2024-03-22T07:39:20.198941Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
2024-03-22T07:39:20.198947Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.originalFileId
2024-03-22T07:39:20.198949Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
2024-03-22T07:39:20.198953Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.originalFileName
2024-03-22T07:39:20.198955Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
2024-03-22T07:39:20.198959Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.profile
2024-03-22T07:39:20.198961Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
2024-03-22T07:39:20.198965Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: Path = $.targetFormat
2024-03-22T07:39:20.198967Z DEBUG ThreadId(01) pact_ffi::mock_server::bodies: detected pact:matcher:type, will configure a matcher
[08:39:20.199] INFO (24436): pact@12.1.2: Verifying message
[08:39:20.199] DEBUG (24436): pact@12.1.2: reified message raw: raw
[08:39:20.199] DEBUG (24436): pact@12.1.2: rehydrated message body into correct type: [object Object]
2024-03-22T07:39:20.201618Z DEBUG ThreadId(01) pact_ffi::mock_server::handles: pact_ffi::mock_server::handles::pactffi_pact_handle_write_file FFI function invoked
2024-03-22T07:39:20.201664Z DEBUG ThreadId(01) pact_models::pact: Merging pact with file "/home/user/project/pacts/somePact.json"
2024-03-22T07:39:20.201750Z  WARN ThreadId(01) pact_models::pact: Note: Existing pact is an older specification version (V3), and will be upgraded
mefellows commented 3 months ago

Thanks for spotting, this is indeed a bug. It looks like pactffiMessageGivenWithParams was not exported by the C interface in @pact-foundation/pact-core.

I'll push out a release shortly that pins the versions so that an upgrade will automatically pull in the correct version.

mefellows commented 3 months ago

Fixed in 12.3.1 via 34d8a4b.