Hello people, Trying to do equality matching of t...
# pact-js
h
Hello people, Trying to do equality matching of the response body , but using Matcher doesn't provide any equality matching ability. Is there any workaround or am i missing something on the matcher ??
Copy code
"matchingRules": {
  "$.body": {
    "match": "equality"
  }
}
m
can you please share your test details? If you just want to match the exact body, you don’t need to use a matcher - it’s effectively “equality” by default
why do you want to do a strict match, may I ask?
h
@Matt (pactflow.io / pact-js / pact-go), we are using strict match because we are using the same set of data at both provider and consumer ends, and data matching is somewhat a requirement .
"interactions": [ { "description": "Request to save theme", "providerState": "Request to save themes", "request": { "method": "POST", "path": "/themes-service/save", "headers": { "Accept": "application/json" } }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "body": { "results": [ { "name": "abc", "result": true } ] }, "matchingRules": { "$.body.: { "match": "type" } } } ]
we are using jest library to generate pact contract json files , which is not including the equality matching rule by default
m
I need to see your test please
my guess is you’re wrapping the body in a matcher such as
like
just don’t use the matcher
h
Copy code
import { HTTPMethod } from "@pact-foundation/pact/src/common/request";
import { Matchers } from "@pact-foundation/pact";
import { pactWith } from "jest-pact";
import fetch from "node-fetch";
import { URL } from "url";

jest.setTimeout(30000);

pactWith(
  { consumer: "aggregator", provider: "theme_provider" },
  (provider) => {
    describe("Theme related end points", () => {
      it("Request to save themes", async () => {
        const url = new URL(
          `${provider.mockService.baseUrl}/themes-service/themes`
        );
        const { validateExample } = Matchers;

        const interaction = {
          state: "Request to save themes",
          uponReceiving: "Request to save themes",
          withRequest: {
            method: <http://HTTPMethod.POST|HTTPMethod.POST>,
            path: url.pathname,

            headers: { Accept: "application/json" },
          },
          willRespondWith: {
            status: 200,
            headers: { "Content-Type": "application/json" },
            body: {
              results: [{ handle: "abc", result: "Created" }],
            },
          },
        };

        await provider.addInteraction(interaction);
        const result = await fetch(
          `${provider.mockService.baseUrl}/themes-service/themes`,
          {
            method: <http://HTTPMethod.POST|HTTPMethod.POST>,
            headers: {
              Accept: "application/json",
              "Content-Type": "application/json",
            },
          }
        );

        expect(await result.json()).toEqual({
          results: [
            {
              handle: "abc",
              result: "Created",
            },
          ],
        });
      });

     
    });
  }
);
Yeah I had wrapped the entire body with like matcher because the generated pact json didn't have any matching rules when using the above
m
matching rules aren’t needed for exact match. Did that work?
h
@Matt (pactflow.io / pact-js / pact-go), No it didn't work. The generated pact json was as given below. It is passing at the consumer test , but when json is used by a provider (Spring boot application), it basically ignores the validation, as there are no matching rules condition. When manually the equality matching rule is added it is passing though
Copy code
{
  "consumer": {
    "name": "aggregator"
  },
  "provider": {
    "name": "theme_provider"
  },
  "interactions": [
    {
      "description": "Request to save themes",
      "providerState": "Request to save themes",
      "request": {
        "method": "POST",
        "path": "/themes-service/themes",
        "headers": {
          "Accept": "application/json"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": {
          "results": [
            {
              "handle": "abc",
              "result": "Created"
            }
          ]
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}
m
, but when json is used by a provider (Spring boot application), it basically ignores the validation, as there are no matching rules condition. When manually the equality matching rule is added it is passing though
That’s not how it works, something else must be wrong. Can you please share the actual problem you’re having?