Search code examples
pact

Pact exact match of a field within an array


I am writing a pact test for a request that filters out the response data by a certain field, hense I would like to create a matcher that would match an array of objects with an exact match on that field.

I tried doing the following two aproaches:

body: eachLike({
  color: 'red',
  name: like('any'),
}),

body: eachLike({
  color: extractPayload('red'),
  name: like('any'),
}),

Bot both of them produce the same result:

"matchingRules": {
  "$.body": {
    "min": 1
  },
  "$.body[*].*": {
    "match": "type"
  },
  "$.body[*].name": {
    "match": "type"
  }
}

It seems to me that having "$.body[*].*": {"match": "type"} in there negates the exact matching for the color field. Am I wrong in that assumption, or is there a correct approach that would resolve this issue?


Solution

  • Yes, the issue is that the type matching is cascading and is not being reset.

    The equal matcher (V3 only) will reset the matching rule for this context.

    It's available in the latest beta: https://github.com/pact-foundation/pact-js/tree/feat/v3.0.0#using-the-v3-matching-rules

    To work it in v2, I would use the regex that matches a single string value here.