Hi, I'm trying to write a pact for an API using Pa...
# pact-js
e
Hi, I'm trying to write a pact for an API using PactJS and TypeScript. The API uses Map, where the Key is an ID (uuid) and the Value will be an object I need to match. As it's unknown what the ID is I can't really write the body out in the
addInteraction()
method (
willRespondWith
). But the resulting pact-json file would have the expressiveness to do what I want. I can't find a matcher that would yield the
matchingRules
that I'm looking for. As an example: Instead of
Copy code
"$.body.angebote.CLASSIC.angebotStatusFachlich": {
   "match": "type"
},
"$.body.angebote.COMFORT.angebotStatusFachlich": {
   "match": "type"
},
I would like it to generate:
Copy code
"$.body.angebote.*.angebotStatusFachlich": {
   "match": "type"
},
Another example:
Copy code
merkmale: {
    DOMIZILLAND_VN: {
        art: 400054,
        code: 40,
    },
    MERKMAL_PARTNERART_VN: {
        art: 432,
        code: 433,
    },
},
=> The keys will vary (DOMIZILLAND_VN, MERKMAL_PARTNERART_VN), so I would like to be able to generate rules like:
Copy code
"$.body.merkmale.*.code": {
   "match": "type"
},
We're currently using Pact 9.x - anything in there that would allow me to write the pact-test in a spec-File that will actually produce matching rules with wildcards? Manually writing the pact-files is not really maintainable. Would there be a solution in 10.x? I saw on github discussions about eachKey-Matchers and I think that goes in the right direction, but I could not find that in the API for 9.x (I read that this kind of API is supposedly bad design, but it works really good with JavaScript/TS Frontends and it is now what it is.) Any help is appreciated.
m
You’re right, 9.x.x won’t have this matcher
There is a matcher in the 10.x.x branch, but there is currently a bug in that (easily fixed): https://github.com/pact-foundation/pact-js/issues/952#issuecomment-1452805742
e
eachValueLike()
sounds exactly like the thing I need.
So I will have to figure out how we can switch to 10.x. Can I use 10.x to generate v2-Pact-Files? Or can I use
eachValueLike
only with v3 Pact Files?
If I can generate v2-Files, the Producer does not necessarily have to update
t
Can I use 10.x to generate v2-Pact-Files?
Yes, but not if you use the
eachValueLike
matcher.
👍 1
e
I switched to pact-js v11 and now I can use
eachKeyLike
(could not find an
eachValueLike
except in a github-issue discussion) It seems like the resulting
matchingRules
are broken. Is that a know issue? I have the following Interaction:
Copy code
await provider
        .given('Server is healthy')
        .uponReceiving('a request to create a new Angebot for Rechtsschutzversicherung-Unternehmen')
        .withRequest({
            method: 'POST',
            path: `/vvn/kaufen-b2c/rest/angebote/rechtsschutzversicherung-unternehmen/v2/neugeschaeft`,
            query: { forceCaptcha: 'false' },
            headers: {
                'x-user-id': 'Z123456',
                'x-caller': 'pact-verification',
                'x-fall-id': fallid,
                'Content-Type': 'application/json',
            },
            body: angeboteErstellenNeugeschaeftRsvUnternehmen as any,
        })
        .willRespondWith({
            status: 200,
            headers: {
                'Content-Type': 'application/json',
            },
            body: {
                angebote: MatchersV3.eachKeyLike('CLASSIC', {
                    angebotStatusTechnisch: MatchersV3.string('TECHNISCH_KONSISTENT'),
                }),
            },
        });
});
Resulting
matchingRules
:
Copy code
"matchingRules": {
  "body": {
    "$.angebote": {
      "combine": "AND",
      "matchers": [
        {
          "match": "values"
        }
      ]
    },
    "$.angebote.*.*": {
      "combine": "AND",
      "matchers": [
        {
          "match": "type"
        }
      ]
    }
  },
  "header": {}
},
$.angebote.*.*
is wrong. It should IMHO be:`$.angebote.*.angebotStatusTechnisch`
Adding more properties in eachKeyLike-Template resultet in:
"$.angebote.*.*": { "combine": "AND", "matchers": [ { "match": "type" }, { "match": "type" }, { "match": "type" }, { "match": "type" }, { "match": "type" }, { "match": "type" }, { "match": "type" }, { "match": "type" } ] },
I don't know where to start searching for the error. I'm trying to compile a simple example.
The path for matchingRules and for generators seems utterly wrong:
Copy code
"generators": {
  "body": {
    "$.angebote.*.*.*": {
      "type": "Uuid"
    },
    "$.angebote.*.*.*.*.*": {
      "max": 10,
      "min": 0,
      "type": "RandomInt"
    },
    "$.angebote.*.*.*[*].*": {
      "type": "Uuid"
    },